﻿using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using Abp.UI;
using Camc.Quality.Instruction.InstructionForm;
using Camc.Quality.InstructionDto;
using Camc.Quality.Quality.TransformHtml;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Linq.Dynamic.Core;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using System.IO;
using Camc.Quality.Quality;
using ICSharpCode.SharpZipLib.Zip;
using Camc.Quality.InstructionDto.Dto;
using Camc.Quality.Bop;
using Camc.Quality.RecordTable.FormBase;
using Camc.Quality.Dto.TechniqueEntityDto;
using System.Linq.Expressions;
using Camc.Quality.Dto.TableDto;
using System.Reflection;
using Abp.Collections.Extensions;

using System.Text;
using Camc.Quality.QualityAnalysis.RFormBase;
using Camc.Quality.QualityAnalysis.RTechniqueEntity;
using Camc.Quality.Dto.TableReturnDto.Technique;
using Camc.Quality.Dto.TableDto.RbopDto;
using Camc.Quality.Dto.ZuoYeReturnDto;
using Camc.Quality.InstructionDto.RDto;
using System.Net;
using System.Drawing;

namespace Camc.Quality.Instruction
{
    public class InstructionAppService : QualityAppServiceBase
    {
        private readonly IRepository<ProcessesEntity, Guid> _processesEntityRepository;//工序
        private readonly IRepository<RProcessesEntity, Guid> _rprocessesEntityRepository;
        private readonly IRepository<FormRelationEntity, Guid> _formRelationEntityRepository;
        private readonly IRepository<InstructionEntity, Guid> _instructionEntityRepository;
        private readonly IRepository<FormInstructionRelation, Guid> _formInstructionRelationRepository;
        private readonly IRepository<FormInfo, Guid> _formInfoRepository;
        private readonly IRepository<DingLiangAndMultiMediaExaminationFormEntity, Guid> _dingLiangAndMultiMediaExaminationFormEntityRepository;
        private readonly IRepository<DingLiangExaminationFormEntity, Guid> _dingLiangExaminationFormEntityRepository;
        private readonly IRepository<DingXingExaminationFormEntity, Guid> _dingXingExaminationFormEntityRepository;
        private readonly IRepository<ElectricalConnectorFormEntity, Guid> _electricalConnectorFormEntityRepository;
        private readonly IRepository<MaterialObjectFormEntity, Guid> _materialObjectFormEntityRepository;
        private readonly IRepository<MomentRationExaminationFormEntity, Guid> _momentRationExaminationFormEntityRepository;
        private readonly IRepository<MultiMediaExaminationFormEntity, Guid> _multiMediaExaminationFormEntityRepository;
        private readonly IRepository<TestRationExaminationFormEntity, Guid> _testRationExaminationFormEntityRepository;
        private readonly IRepository<BopNode, Guid> _bopNodeRepository;
        private readonly IRepository<RbopNode, Guid> _rbopNodeRepository;
        private readonly IRepository<BopDesc, Guid> _bopDescRepository;
        private readonly IRepository<RbopDesc, Guid> _rbopDescRepository;
        private readonly IRepository<TechniqueEntity, Guid> _techniqueEntityRepository;
        private readonly IRepository<RTechniqueEntity, Guid> _rtechniqueEntityRepository;
        private readonly IRepository<TableBase, Guid> _tableBaseRepository;
        private readonly IRepository<RTableBase, Guid> _rtableBaseRepository;
        private readonly IRepository<FormColRelationEntity, Guid> _formColRelationEntityRepository;

        public InstructionAppService(IRepository<InstructionEntity, Guid> instructionEntityRepository,
                                         IRepository<FormInstructionRelation, Guid> formInstructionRelationRepository,
                                         IRepository<FormInfo, Guid> formInfoRepository,
                                         IRepository<DingLiangAndMultiMediaExaminationFormEntity, Guid> dingLiangAndMultiMediaExaminationFormEntityRepository,
                                         IRepository<DingLiangExaminationFormEntity, Guid> dingLiangExaminationFormEntityRepository,
                                         IRepository<DingXingExaminationFormEntity, Guid> dingXingExaminationFormEntityRepository,
                                         IRepository<ElectricalConnectorFormEntity, Guid> electricalConnectorFormEntityRepository,
                                         IRepository<MaterialObjectFormEntity, Guid> materialObjectFormEntityRepository,
                                         IRepository<MomentRationExaminationFormEntity, Guid> momentRationExaminationFormEntityRepository,
                                         IRepository<MultiMediaExaminationFormEntity, Guid> multiMediaExaminationFormEntityRepository,
                                         IRepository<TestRationExaminationFormEntity, Guid> testRationExaminationFormEntityRepository,
                                         IRepository<BopNode, Guid> bopNodeRepository,
                                         IRepository<BopDesc, Guid> bopDescRepository,
                                         IRepository<TechniqueEntity, Guid> techniqueEntityRepository,
                                         IRepository<TableBase, Guid> tableBaseRepository,
                                         IRepository<FormRelationEntity, Guid> formRelationEntityRepository,
                                         IRepository<ProcessesEntity, Guid> processesEntityRepository,
                                         IRepository<FormColRelationEntity, Guid> formColRelationEntityRepository,
                                         IRepository<RbopNode, Guid> rbopNodeRepository,
                                         IRepository<RbopDesc, Guid> rbopDescRepository,
                                         IRepository<RTechniqueEntity, Guid> rtechniqueEntityRepository,
                                         IRepository<RTableBase, Guid> rtableBaseRepository,
                                          IRepository<RProcessesEntity, Guid> rprocessesEntityRepository

            )
        {
            _instructionEntityRepository = instructionEntityRepository;
            _formInstructionRelationRepository = formInstructionRelationRepository;
            _formInfoRepository = formInfoRepository;
            _dingLiangAndMultiMediaExaminationFormEntityRepository = dingLiangAndMultiMediaExaminationFormEntityRepository;
            _dingLiangExaminationFormEntityRepository = dingLiangExaminationFormEntityRepository;
            _dingXingExaminationFormEntityRepository = dingXingExaminationFormEntityRepository;
            _electricalConnectorFormEntityRepository = electricalConnectorFormEntityRepository;
            _materialObjectFormEntityRepository = materialObjectFormEntityRepository;
            _momentRationExaminationFormEntityRepository = momentRationExaminationFormEntityRepository;
            _multiMediaExaminationFormEntityRepository = multiMediaExaminationFormEntityRepository;
            _testRationExaminationFormEntityRepository = testRationExaminationFormEntityRepository;
            _bopNodeRepository = bopNodeRepository;
            _bopDescRepository = bopDescRepository;
            _tableBaseRepository = tableBaseRepository;
            _techniqueEntityRepository = techniqueEntityRepository;
            _formRelationEntityRepository = formRelationEntityRepository;
            _processesEntityRepository = processesEntityRepository;
            _formColRelationEntityRepository = formColRelationEntityRepository;
            _rtechniqueEntityRepository = rtechniqueEntityRepository;
            _rbopDescRepository = rbopDescRepository;
            _rbopNodeRepository = rbopNodeRepository;
            _rtableBaseRepository = rtableBaseRepository;
            _rprocessesEntityRepository = rprocessesEntityRepository;
        }



        //[HttpPost]
        //[Route("upload")]
        public async Task InstructionJsonUpload(IFormFile file)
        {
            string uniqueFileName = null;
            string[] formatArray = { ".zip" };
            if (file != null)
            {

                //文件格式
                var fileExtension = Path.GetExtension(file.FileName);
                if (!formatArray.Contains(fileExtension))
                {
                    throw new UserFriendlyException("上传文件格式错误");
                }


                //文件名称
                var jsonName = file.FileName.Split('.')[0];
                //读取文件保存的根目录
                string fileSaveRootDir = ConfigHelper.GetAppSetting("App", "UploadInstructionFile");


                //文件保存的路径(应用的工作目录+文件夹相对路径);
                string fileSavePath = Environment.CurrentDirectory + "/wwwroot/" + fileSaveRootDir;
                if (!Directory.Exists(fileSavePath))
                {
                    Directory.CreateDirectory(fileSavePath);
                }



                uniqueFileName = jsonName + fileExtension;
                string filePath = Path.Combine(fileSavePath, uniqueFileName);
                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    file.CopyTo(fileStream);
                    fileStream.Flush();
                }

                //获得当前文件夹名称
                var fileSave = uniqueFileName.Split('.')[0];
                //解压文件
                (new FastZip()).ExtractZip(@filePath, @fileSavePath + "/" + fileSave, "");
                //   var josnname = file.FileName.Split('.')[0];
                var Json = fileSavePath + "/" + fileSave + "/" + jsonName + ".json";
                //解析文件
                await InputInstructionJson(Json, jsonName);
            }
            else
            {
                throw new UserFriendlyException("文件不能为空");
            }



        }
        private async Task InputInstructionJson(string filepath, string FileName)
        {
            var dto = await _instructionEntityRepository.FirstOrDefaultAsync(c => c.FileNum == FileName);
            if (dto != null)
            {
                throw new UserFriendlyException("当前系统中已存在" + dto.Name + "的相应信息！");
            }
            // string filepath = @"F:\GCdrawingnumS112001Z2.json";
            try
            {
                var jsonStr = ReadJson.GetFileJson(filepath);

                var OutDto = JsonConvert.DeserializeObject<GetJsonInput>(jsonStr.Replace(" ", "")); //修改去掉json中值的空格

                var instructionEntityList = ObjectMapper.Map<InstructionEntity>(OutDto.ProductInfo);
                instructionEntityList.OriginalId = OutDto.ProductInfo.Id.ToString();
                instructionEntityList.Id = Guid.NewGuid();
                var instructionid = await _instructionEntityRepository.InsertAndGetIdAsync(instructionEntityList);
                if (OutDto.FormInfo.MaterialObjectFormData != null)
                {
                    var materialObjectForm = ObjectMapper.Map<List<MaterialObjectFormEntity>>(OutDto.FormInfo.MaterialObjectFormData.MaterialObjectForm);
                    var formInfo = ObjectMapper.Map<FormInfo>(OutDto.FormInfo.MaterialObjectFormData);
                    formInfo.InstructionEntityId = instructionid;
                    var formInfoid = await _formInfoRepository.InsertAndGetIdAsync(formInfo);
                    foreach (var item1 in materialObjectForm)
                    {
                        item1.FormInfoId = formInfoid;
                        item1.Oid = item1.Id.ToString();
                        item1.Id = Guid.NewGuid();
                        var fid = await _materialObjectFormEntityRepository.InsertAndGetIdAsync(item1);
                        FormInstructionRelation formInstructionRelation = new FormInstructionRelation();
                        formInstructionRelation.FormId = fid;
                        formInstructionRelation.InstructionEntityId = instructionid;
                        await _formInstructionRelationRepository.InsertAsync(formInstructionRelation);
                    }
                }

                if (OutDto.FormInfo.QuantitativeExaminationFormData != null)
                {
                    var dingLiangExaminationForm = ObjectMapper.Map<List<DingLiangExaminationFormEntity>>(OutDto.FormInfo.QuantitativeExaminationFormData.QuantitativeExaminationForm);
                    var dingLiangExaminationFormformInfo = ObjectMapper.Map<FormInfo>(OutDto.FormInfo.QuantitativeExaminationFormData);
                    dingLiangExaminationFormformInfo.InstructionEntityId = instructionid;
                    var dingLiangExaminationFormformInfoid = await _formInfoRepository.InsertAndGetIdAsync(dingLiangExaminationFormformInfo);
                    foreach (var item1 in dingLiangExaminationForm)
                    {
                        item1.FormInfoId = dingLiangExaminationFormformInfoid;
                        item1.Oid = item1.Id.ToString();
                        item1.Id = Guid.NewGuid();

                        var fid = await _dingLiangExaminationFormEntityRepository.InsertAndGetIdAsync(item1);
                        FormInstructionRelation formInstructionRelation = new FormInstructionRelation();
                        formInstructionRelation.FormId = fid;
                        formInstructionRelation.InstructionEntityId = instructionid;
                        await _formInstructionRelationRepository.InsertAsync(formInstructionRelation);
                    }
                }

                if (OutDto.FormInfo.QualitativeExaminationFormData != null)
                {
                    var dingXingExaminationForm = ObjectMapper.Map<List<DingXingExaminationFormEntity>>(OutDto.FormInfo.QualitativeExaminationFormData.QualitativeExaminationForm);
                    var dingXingExaminationFormformInfo = ObjectMapper.Map<FormInfo>(OutDto.FormInfo.QualitativeExaminationFormData);
                    dingXingExaminationFormformInfo.InstructionEntityId = instructionid;
                    var dingXingExaminationFormFormformInfoid = await _formInfoRepository.InsertAndGetIdAsync(dingXingExaminationFormformInfo);
                    foreach (var item1 in dingXingExaminationForm)
                    {
                        item1.FormInfoId = dingXingExaminationFormFormformInfoid;
                        item1.Oid = item1.Id.ToString();
                        item1.Id = Guid.NewGuid();

                        var fid = await _dingXingExaminationFormEntityRepository.InsertAndGetIdAsync(item1);
                        FormInstructionRelation formInstructionRelation = new FormInstructionRelation();
                        formInstructionRelation.FormId = fid;
                        formInstructionRelation.InstructionEntityId = instructionid;
                        await _formInstructionRelationRepository.InsertAsync(formInstructionRelation);
                    }

                }


                if (OutDto.FormInfo.MultiMediaExaminationFormData != null)
                {
                    var multiMediaExaminationForm = ObjectMapper.Map<List<MultiMediaExaminationFormEntity>>(OutDto.FormInfo.MultiMediaExaminationFormData.MultiMediaExaminationForm);
                    var multiMediaExaminationFormformInfo = ObjectMapper.Map<FormInfo>(OutDto.FormInfo.MultiMediaExaminationFormData);
                    multiMediaExaminationFormformInfo.InstructionEntityId = instructionid;
                    var multiMediaExaminationFormformInfoid = await _formInfoRepository.InsertAndGetIdAsync(multiMediaExaminationFormformInfo);
                    foreach (var item1 in multiMediaExaminationForm)
                    {
                        item1.FormInfoId = multiMediaExaminationFormformInfoid;
                        item1.Oid = item1.Id.ToString();
                        item1.Id = Guid.NewGuid();
                        var fid = await _multiMediaExaminationFormEntityRepository.InsertAndGetIdAsync(item1);
                        FormInstructionRelation formInstructionRelation = new FormInstructionRelation();
                        formInstructionRelation.FormId = fid;
                        formInstructionRelation.InstructionEntityId = instructionid;
                        await _formInstructionRelationRepository.InsertAsync(formInstructionRelation);
                    }

                }
                if (OutDto.FormInfo.QuantitativeAndMultiMediaExaminationFormData != null)
                {

                    var dingLiangAndMultiMediaExaminationForm = ObjectMapper.Map<List<DingLiangAndMultiMediaExaminationFormEntity>>(OutDto.FormInfo.QuantitativeAndMultiMediaExaminationFormData.QuantitativeAndMultiMediaExaminationForm);
                    var dingLiangAndMultiMediaExaminationFormformInfo = ObjectMapper.Map<FormInfo>(OutDto.FormInfo.QuantitativeAndMultiMediaExaminationFormData);
                    dingLiangAndMultiMediaExaminationFormformInfo.InstructionEntityId = instructionid;
                    var dingLiangAndMultiMediaExaminationFormformInfoid = await _formInfoRepository.InsertAndGetIdAsync(dingLiangAndMultiMediaExaminationFormformInfo);
                    foreach (var item1 in dingLiangAndMultiMediaExaminationForm)
                    {
                        item1.FormInfoId = dingLiangAndMultiMediaExaminationFormformInfoid;
                        item1.Oid = item1.Id.ToString();
                        item1.Id = Guid.NewGuid();

                        var fid = await _dingLiangAndMultiMediaExaminationFormEntityRepository.InsertAndGetIdAsync(item1);
                        FormInstructionRelation formInstructionRelation = new FormInstructionRelation();
                        formInstructionRelation.FormId = fid;
                        formInstructionRelation.InstructionEntityId = instructionid;
                        await _formInstructionRelationRepository.InsertAsync(formInstructionRelation);
                    }
                }
                if (OutDto.FormInfo.MomentRationExaminationFormData != null)
                {
                    var momentRationExaminationForm = ObjectMapper.Map<List<MomentRationExaminationFormEntity>>(OutDto.FormInfo.MomentRationExaminationFormData.MomentRationExaminationForm);
                    var momentRationExaminationFormformInfo = ObjectMapper.Map<FormInfo>(OutDto.FormInfo.MomentRationExaminationFormData);
                    momentRationExaminationFormformInfo.InstructionEntityId = instructionid;
                    var momentRationExaminationFormformInfoid = await _formInfoRepository.InsertAndGetIdAsync(momentRationExaminationFormformInfo);
                    foreach (var item1 in momentRationExaminationForm)
                    {
                        item1.FormInfoId = momentRationExaminationFormformInfoid;
                        item1.Oid = item1.Id.ToString();
                        item1.Id = Guid.NewGuid();
                        var fid = await _momentRationExaminationFormEntityRepository.InsertAndGetIdAsync(item1);
                        FormInstructionRelation formInstructionRelation = new FormInstructionRelation();
                        formInstructionRelation.FormId = fid;
                        formInstructionRelation.InstructionEntityId = instructionid;
                        await _formInstructionRelationRepository.InsertAsync(formInstructionRelation);
                    }

                }
                if (OutDto.FormInfo.TestRationExaminationFormData != null)
                {

                    var testRationExaminationForm = ObjectMapper.Map<List<TestRationExaminationFormEntity>>(OutDto.FormInfo.TestRationExaminationFormData.TestRationExaminationForm);
                    var testRationExaminationFormformInfo = ObjectMapper.Map<FormInfo>(OutDto.FormInfo.TestRationExaminationFormData);
                    testRationExaminationFormformInfo.InstructionEntityId = instructionid;
                    var testRationExaminationFormformInfoid = await _formInfoRepository.InsertAndGetIdAsync(testRationExaminationFormformInfo);
                    foreach (var item1 in testRationExaminationForm)
                    {
                        item1.FormInfoId = testRationExaminationFormformInfoid;
                        item1.Oid = item1.Id.ToString();
                        item1.Id = Guid.NewGuid();
                        var fid = await _testRationExaminationFormEntityRepository.InsertAndGetIdAsync(item1);
                        FormInstructionRelation formInstructionRelation = new FormInstructionRelation();
                        formInstructionRelation.FormId = fid;
                        formInstructionRelation.InstructionEntityId = instructionid;
                        await _formInstructionRelationRepository.InsertAsync(formInstructionRelation);
                    }
                }
                if (OutDto.FormInfo.ElectricalConnectorFormData != null)
                {
                    var electricalConnectorForm = ObjectMapper.Map<List<ElectricalConnectorFormEntity>>(OutDto.FormInfo.ElectricalConnectorFormData.ElectricalConnectorForm);
                    var electricalConnectorFormformInfo = ObjectMapper.Map<FormInfo>(OutDto.FormInfo.ElectricalConnectorFormData);
                    electricalConnectorFormformInfo.InstructionEntityId = instructionid;
                    var electricalConnectorFormformInfoid = await _formInfoRepository.InsertAndGetIdAsync(electricalConnectorFormformInfo);
                    foreach (var item1 in electricalConnectorForm)
                    {
                        item1.FormInfoId = electricalConnectorFormformInfoid;
                        item1.Oid = item1.Id.ToString();
                        item1.Id = Guid.NewGuid();
                        var fid = await _electricalConnectorFormEntityRepository.InsertAndGetIdAsync(item1);
                        FormInstructionRelation formInstructionRelation = new FormInstructionRelation();
                        formInstructionRelation.FormId = fid;
                        formInstructionRelation.InstructionEntityId = instructionid;
                        await _formInstructionRelationRepository.InsertAsync(formInstructionRelation);
                    }
                }


            }
            catch (Exception e)
            {
                throw e;
            }

        }

        public async Task<PagedResultDto<GetProductInfoOutputDto>> GetGetProductInfoList(GetInstructionInput input)
        {
            var query = _instructionEntityRepository.GetAll()
                 .WhereIf(!input.Filter.IsNullOrWhiteSpace(),
                 x => x.Model.Contains(input.Filter)
                 );
            var count = await query.CountAsync();
            var instructionlist = await query.OrderBy(input.Sorting).PageBy(input).ToListAsync();
            var list = ObjectMapper.Map<List<GetProductInfoOutputDto>>(instructionlist);
            foreach (var item in list)
            {
                var user = await UserManager.GetUserByIdAsync(item.CreatorUserId);
                if (user != null)
                {
                    item.CreationUserName = user.Surname + user.Name;
                }
            }
            return new PagedResultDto<GetProductInfoOutputDto>(count, list);
        }

        /// <summary>
        /// 实物配套表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<GetMaterialObjectFormEntityDto>> GetMaterialObjectFormList(GetFormInput input)
        {
            var a = await _formInfoRepository.FirstOrDefaultAsync(p => p.InstructionEntityId == input.Id && p.FormName == "实物配套表");
            var count = 0;
            var list = new List<GetMaterialObjectFormEntityDto>();
            if (a != null)
            {
                var query = _materialObjectFormEntityRepository.GetAll().Where(p => p.FormInfoId == a.Id)
                .WhereIf(!input.Filter.IsNullOrWhiteSpace(),
                x => x.PartDrawNo.Contains(input.Filter)
                );
                count = await query.CountAsync();
                var instructionlist = await query.OrderBy(p => p.OrderNo).PageBy(input).ToListAsync();
                list = ObjectMapper.Map<List<GetMaterialObjectFormEntityDto>>(instructionlist);
                foreach (var item in list)
                {
                    var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == item.Id);
                    item.FormInstructionRelation = ObjectMapper.Map<GetFormInstructionRelationDto>(formInstruction);

                }
            }

            return new PagedResultDto<GetMaterialObjectFormEntityDto>(count, list);
        }

        /// <summary>
        /// 定量检查表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<GetDingLiangExaminationFormEntityDto>> GetDingLiangExaminationFormList(GetFormInput input)
        {
            var a = await _formInfoRepository.FirstOrDefaultAsync(p => p.InstructionEntityId == input.Id && p.FormName == "定量检查表");
            var count = 0;
            var list = new List<GetDingLiangExaminationFormEntityDto>();
            if (a != null)
            {
                var query = _dingLiangExaminationFormEntityRepository.GetAll().Where(p => p.FormInfoId == a.Id)
                .WhereIf(!input.Filter.IsNullOrWhiteSpace(),
                x => x.PartDrawNo.Contains(input.Filter)
                );
                count = await query.CountAsync();
                var instructionlist = await query.OrderBy(p => p.OrderNo).PageBy(input).ToListAsync();
                list = ObjectMapper.Map<List<GetDingLiangExaminationFormEntityDto>>(instructionlist);
                foreach (var item in list)
                {
                    var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == item.Id);
                    item.FormInstructionRelation = ObjectMapper.Map<GetFormInstructionRelationDto>(formInstruction);

                    item.Operators = new List<UserSignatureInfoDto>();
                    item.MutualInspectors = new List<UserSignatureInfoDto>();
                    item.Inspectors = new List<UserSignatureInfoDto>();
                    var data = GetUserSignatureInfo(item.Operator, item.MutualInspector, item.Inspector);
                    if (data.First != null)
                    {
                        item.Operators = data.First;
                    }
                    if (data.Second != null)
                    {
                        item.MutualInspectors = data.Second;
                    }
                    if (data.Third != null)
                    {
                        item.Inspectors = data.Third;
                    }
                    //if (data!=null)
                    //{
                    //    item.Operators = data.First;
                    //    item.MutualInspectors = data.Second;
                    //    item.Inspectors = data.Third;
                    //}


                }
            }

            return new PagedResultDto<GetDingLiangExaminationFormEntityDto>(count, list);
        }


        /// <summary>
        /// 定性检查表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<GetDingXingExaminationFormEntityDto>> GetDingXingExaminationFormList(GetFormInput input)
        {
            var a = await _formInfoRepository.FirstOrDefaultAsync(p => p.InstructionEntityId == input.Id && p.FormName == "定性检查表");
            var count = 0;
            var list = new List<GetDingXingExaminationFormEntityDto>();
            if (a != null)
            {
                var query = _dingXingExaminationFormEntityRepository.GetAll().Where(p => p.FormInfoId == a.Id)
                .WhereIf(!input.Filter.IsNullOrWhiteSpace(),
                x => x.PartDrawNo.Contains(input.Filter)
                );
                count = await query.CountAsync();
                var instructionlist = await query.OrderBy(p => p.OrderNo).PageBy(input).ToListAsync();
                list = ObjectMapper.Map<List<GetDingXingExaminationFormEntityDto>>(instructionlist);
                foreach (var item in list)
                {
                    var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == item.Id);
                    item.FormInstructionRelation = ObjectMapper.Map<GetFormInstructionRelationDto>(formInstruction);

                    item.Operators = new List<UserSignatureInfoDto>();
                    item.MutualInspectors = new List<UserSignatureInfoDto>();
                    item.Inspectors = new List<UserSignatureInfoDto>();
                    var data = GetUserSignatureInfo(item.Operator, item.MutualInspector, item.Inspector);
                    if (data.First != null)
                    {
                        item.Operators = data.First;
                    }
                    if (data.Second != null)
                    {
                        item.MutualInspectors = data.Second;
                    }
                    if (data.Third != null)
                    {
                        item.Inspectors = data.Third;
                    }
                    //if (data != null)
                    //{
                    //    item.Operators = data.First;
                    //    item.MutualInspectors = data.Second;
                    //    item.Inspectors = data.Third;
                    //}
                }
            }

            return new PagedResultDto<GetDingXingExaminationFormEntityDto>(count, list);
        }

        #region 弃用  没循环多媒体照片
        //public async Task<PagedResultDto<GetMultiMediaExaminationFormEntityDto>> GetMultiMediaExaminationFormList(GetFormInput input)
        //{
        //    var a = await _formInfoRepository.FirstOrDefaultAsync(p => p.InstructionEntityId == input.Id && p.FormName == "多媒体检查表");
        //    var count = 0;
        //    var list = new List<GetMultiMediaExaminationFormEntityDto>();
        //    if (a != null)
        //    {
        //        var query = _multiMediaExaminationFormEntityRepository.GetAll().Where(p => p.FormInfoId == a.Id)
        //        .WhereIf(!input.Filter.IsNullOrWhiteSpace(),
        //        x => x.PartDrawNo.Contains(input.Filter)
        //        );
        //        count = await query.CountAsync();
        //        var instructionlist = await query.OrderBy(p => p.OrderNo).PageBy(input).ToListAsync();
        //        list = ObjectMapper.Map<List<GetMultiMediaExaminationFormEntityDto>>(instructionlist);
        //        foreach (var item in list)
        //        {
        //            var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == item.Id);
        //            item.FormInstructionRelation = ObjectMapper.Map<GetFormInstructionRelationDto>(formInstruction);
        //        }
        //    }

        //    return new PagedResultDto<GetMultiMediaExaminationFormEntityDto>(count, list);
        //}

        //public async Task<PagedResultDto<GetDingLiangAndMultiMediaDto>> GetDingLiangAndMultiMediaExaminationFormList(GetFormInput input)
        //{
        //    var a = await _formInfoRepository.FirstOrDefaultAsync(p => p.InstructionEntityId == input.Id && p.FormName == "定量&多媒体检查表");
        //    var count = 0;
        //    var list = new List<GetDingLiangAndMultiMediaDto>();
        //    if (a != null)
        //    {
        //        var query = _dingLiangAndMultiMediaExaminationFormEntityRepository.GetAll().Where(p => p.FormInfoId == a.Id)
        //        .WhereIf(!input.Filter.IsNullOrWhiteSpace(),
        //        x => x.PartDrawNo.Contains(input.Filter)
        //        );
        //        count = await query.CountAsync();
        //        var instructionlist = await query.OrderBy(p => p.OrderNo).PageBy(input).ToListAsync();
        //        list = ObjectMapper.Map<List<GetDingLiangAndMultiMediaDto>>(instructionlist);
        //        foreach (var item in list)
        //        {
        //            var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == item.Id);
        //            item.FormInstructionRelation = ObjectMapper.Map<GetFormInstructionRelationDto>(formInstruction);

        //        }
        //    }

        //    return new PagedResultDto<GetDingLiangAndMultiMediaDto>(count, list);
        //}

        //public async Task<PagedResultDto<GetElectricalConnectorFormEntityDto>> GetElectricalConnectorFormList(GetFormInput input)
        //{
        //    var a = await _formInfoRepository.FirstOrDefaultAsync(p => p.InstructionEntityId == input.Id && p.FormName == "电连接器类多媒体");
        //    var count = 0;
        //    var list = new List<GetElectricalConnectorFormEntityDto>();
        //    if (a != null)
        //    {
        //        var query = _electricalConnectorFormEntityRepository.GetAll().Where(p => p.FormInfoId == a.Id)
        //        .WhereIf(!input.Filter.IsNullOrWhiteSpace(),
        //        x => x.PartDrawNo.Contains(input.Filter)
        //        );
        //        count = await query.CountAsync();
        //        var instructionlist = await query.OrderBy(p => p.OrderNo).PageBy(input).ToListAsync();
        //        list = ObjectMapper.Map<List<GetElectricalConnectorFormEntityDto>>(instructionlist);
        //        foreach (var item in list)
        //        {
        //            var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == item.Id);
        //            item.FormInstructionRelation = ObjectMapper.Map<GetFormInstructionRelationDto>(formInstruction);
        //        }
        //    }

        //    return new PagedResultDto<GetElectricalConnectorFormEntityDto>(count, list);
        //}
        #endregion

        /// <summary>
        /// 力矩类定量检查表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<GetMomentRationExaminationFormEntityDto>> GetMomentRationExaminationFormList(GetFormInput input)
        {
            var a = await _formInfoRepository.FirstOrDefaultAsync(p => p.InstructionEntityId == input.Id && p.FormName == "力矩类定量检查表");
            var count = 0;
            var list = new List<GetMomentRationExaminationFormEntityDto>();
            if (a != null)
            {
                var query = _momentRationExaminationFormEntityRepository.GetAll().Where(p => p.FormInfoId == a.Id)
                .WhereIf(!input.Filter.IsNullOrWhiteSpace(),
                x => x.PartDrawNo.Contains(input.Filter)
                );
                count = await query.CountAsync();
                var instructionlist = await query.OrderBy(p => p.OrderNo).PageBy(input).ToListAsync();
                list = ObjectMapper.Map<List<GetMomentRationExaminationFormEntityDto>>(instructionlist);
                foreach (var item in list)
                {
                    var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == item.Id);
                    item.FormInstructionRelation = ObjectMapper.Map<GetFormInstructionRelationDto>(formInstruction);

                    item.Operators = new List<UserSignatureInfoDto>();
                    item.MutualInspectors = new List<UserSignatureInfoDto>();
                    item.Inspectors = new List<UserSignatureInfoDto>();
                    var data = GetUserSignatureInfo(item.Operator, item.MutualInspector, item.Inspector);
                    if (data.First != null)
                    {
                        item.Operators = data.First;
                    }
                    if (data.Second != null)
                    {
                        item.MutualInspectors = data.Second;
                    }
                    if (data.Third != null)
                    {
                        item.Inspectors = data.Third;
                    }
                    //if (data != null)
                    //{
                    //    item.Operators = data.First;
                    //    item.MutualInspectors = data.Second;
                    //    item.Inspectors = data.Third;
                    //}
                }
            }

            return new PagedResultDto<GetMomentRationExaminationFormEntityDto>(count, list);
        }
        /// <summary>
        /// 测试类定量检查表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<GetTestRationExaminationFormEntityDto>> GetTestRationExaminationFormList(GetFormInput input)
        {
            var a = await _formInfoRepository.FirstOrDefaultAsync(p => p.InstructionEntityId == input.Id && p.FormName == "测试类定量检查表");
            var count = 0;
            var list = new List<GetTestRationExaminationFormEntityDto>();
            if (a != null)
            {
                var query = _testRationExaminationFormEntityRepository.GetAll().Where(p => p.FormInfoId == a.Id)
                .WhereIf(!input.Filter.IsNullOrWhiteSpace(),
                x => x.PartDrawNo.Contains(input.Filter)
                );
                count = await query.CountAsync();
                var instructionlist = await query.OrderBy(p => p.OrderNo).PageBy(input).ToListAsync();
                list = ObjectMapper.Map<List<GetTestRationExaminationFormEntityDto>>(instructionlist);
                foreach (var item in list)
                {
                    var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == item.Id);
                    item.FormInstructionRelation = ObjectMapper.Map<GetFormInstructionRelationDto>(formInstruction);

                    item.Operators = new List<UserSignatureInfoDto>();
                    item.MutualInspectors = new List<UserSignatureInfoDto>();
                    item.Inspectors = new List<UserSignatureInfoDto>();
                    var data = GetUserSignatureInfo(item.Operator, item.MutualInspector, item.Inspector);
                    if (data.First != null)
                    {
                        item.Operators = data.First;
                    }
                    if (data.Second != null)
                    {
                        item.MutualInspectors = data.Second;
                    }
                    if (data.Third != null)
                    {
                        item.Inspectors = data.Third;
                    }
                    //if (data != null)
                    //{
                    //    item.Operators = data.First;
                    //    item.MutualInspectors = data.Second;
                    //    item.Inspectors = data.Third;
                    //}
                }
            }

            return new PagedResultDto<GetTestRationExaminationFormEntityDto>(count, list);
        }

        /// <summary>
        /// 获取树
        /// </summary>
        /// <returns></returns>
        public async Task<List<GetInstructionExaminationDto>> GetGetInstructionExaminationDto()
        {
            var xh = await _instructionEntityRepository.GetAll().GroupBy(p => p.Model).ToListAsync();
            List<GetInstructionExaminationDto> getInstructionExaminationDtos = new List<GetInstructionExaminationDto>();
            foreach (var item in xh)
            {
                GetInstructionExaminationDto getInstructionExaminationDto = new GetInstructionExaminationDto();
                getInstructionExaminationDto.Name = item.Key;
                getInstructionExaminationDto.LevelNum = 1;
                var fcs = await _instructionEntityRepository.GetAll().Where(p => p.Model == item.Key).GroupBy(p => p.FaCi).ToListAsync();
                List<GetInstructionExaminationDto> fclist = new List<GetInstructionExaminationDto>();
                foreach (var fc in fcs)
                {
                    GetInstructionExaminationDto f = new GetInstructionExaminationDto();
                    f.Name = fc.Key;
                    f.LevelNum = 2;
                    var data = await _instructionEntityRepository.GetAllListAsync(p => p.Model == item.Key && p.FaCi == fc.Key);
                    List<GetInstructionExaminationDto> datalist = new List<GetInstructionExaminationDto>();
                    foreach (var ds in data)
                    {
                        GetInstructionExaminationDto d = new GetInstructionExaminationDto();
                        d.Name = ds.Name + "(" + ds.Version + ")";
                        d.FileNum = ds.FileNum;
                        d.Id = ds.Id;
                        d.LevelNum = 3;
                        datalist.Add(d);
                    }
                    f.children = datalist;
                    fclist.Add(f);
                }
                getInstructionExaminationDto.children = fclist;
                getInstructionExaminationDtos.Add(getInstructionExaminationDto);
            }
            return getInstructionExaminationDtos;
        }

        /// <summary>
        /// 多媒体检查表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<GetJSMultiMediaExaminationFormEntityDto>> GetJSMultiMediaExaminationFormList(GetFormInput input)
        {
            var a = await _formInfoRepository.FirstOrDefaultAsync(p => p.InstructionEntityId == input.Id && p.FormName == "多媒体检查表");
            var count = 0;
            var list = new List<GetJSMultiMediaExaminationFormEntityDto>();
            if (a != null)
            {
                var query = _multiMediaExaminationFormEntityRepository.GetAll().Where(p => p.FormInfoId == a.Id)
                .WhereIf(!input.Filter.IsNullOrWhiteSpace(),
                x => x.PartDrawNo.Contains(input.Filter)
                );
                count = await query.CountAsync();
                var instructionlist = await query.OrderBy(p => p.OrderNo).PageBy(input).ToListAsync();
                list = ObjectMapper.Map<List<GetJSMultiMediaExaminationFormEntityDto>>(instructionlist);
                foreach (var item in list)
                {
                    var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == item.Id);
                    item.FormInstructionRelation = ObjectMapper.Map<GetFormInstructionRelationDto>(formInstruction);
                    List<MediaFileList> mediaFileLists = new List<MediaFileList>();
                    if (item.MediaFileName != null && item.MediaFileName != "")
                    {
                        var fileNames = item.MediaFileName.Split(',');
                        foreach (var fileName in fileNames)
                        {
                            var types = fileName.Split('.');
                            MediaFileList mediaFileList = new MediaFileList();
                            mediaFileList.MediaFileName = fileName;
                            mediaFileList.Type = types[1];
                            mediaFileLists.Add(mediaFileList);

                        }
                    }
                    item.MediaFileLists = mediaFileLists;

                    item.Operators = new List<UserSignatureInfoDto>();
                    item.Confirmers = new List<UserSignatureInfoDto>();
                    item.MutualInspectors = new List<UserSignatureInfoDto>();
                    var data = GetUserSignatureInfo(item.Operator, item.Confirmer, item.MutualInspector);
                    if (data.First != null)
                    {
                        item.Operators = data.First;
                    }
                    if (data.Second != null)
                    {
                        item.Confirmers = data.Second;
                    }
                    if (data.Third != null)
                    {
                        item.MutualInspectors = data.Third;
                    }
                    //if (data != null)
                    //{
                    //    item.Operators = data.First;
                    //    item.Confirmers = data.Second;
                    //    item.MutualInspectors = data.Third;
                    //}
                }
            }

            return new PagedResultDto<GetJSMultiMediaExaminationFormEntityDto>(count, list);
        }
        /// <summary>
        /// 定量&多媒体检查表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<GetJSDingLiangAndMultiMediaDto>> GetJSDingLiangAndMultiMediaExaminationFormList(GetFormInput input)
        {
            var a = await _formInfoRepository.FirstOrDefaultAsync(p => p.InstructionEntityId == input.Id && p.FormName == "定量&多媒体检查表");
            var count = 0;
            var list = new List<GetJSDingLiangAndMultiMediaDto>();
            if (a != null)
            {
                var query = _dingLiangAndMultiMediaExaminationFormEntityRepository.GetAll().Where(p => p.FormInfoId == a.Id)
                .WhereIf(!input.Filter.IsNullOrWhiteSpace(),
                x => x.PartDrawNo.Contains(input.Filter)
                );
                count = await query.CountAsync();
                var instructionlist = await query.OrderBy(p => p.OrderNo).PageBy(input).ToListAsync();
                list = ObjectMapper.Map<List<GetJSDingLiangAndMultiMediaDto>>(instructionlist);
                foreach (var item in list)
                {
                    var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == item.Id);
                    item.FormInstructionRelation = ObjectMapper.Map<GetFormInstructionRelationDto>(formInstruction);

                    List<MediaFileList> mediaFileLists = new List<MediaFileList>();
                    if (item.MediaFileName != null && item.MediaFileName != "")
                    {
                        var fileNames = item.MediaFileName.Split(',');
                        foreach (var fileName in fileNames)
                        {
                            var types = fileName.Split('.');
                            MediaFileList mediaFileList = new MediaFileList();
                            mediaFileList.MediaFileName = fileName;
                            mediaFileList.Type = types[1];
                            mediaFileLists.Add(mediaFileList);

                        }
                    }
                    item.MediaFileLists = mediaFileLists;

                    item.Operators = new List<UserSignatureInfoDto>();
                    item.MutualInspectors = new List<UserSignatureInfoDto>();
                    item.Inspectors = new List<UserSignatureInfoDto>();
                    var data = GetUserSignatureInfo(item.Operator, item.MutualInspector, item.Inspector);
                    if (data.First != null)
                    {
                        item.Operators = data.First;
                    }
                    if (data.Second != null)
                    {
                        item.MutualInspectors = data.Second;
                    }
                    if (data.Third != null)
                    {
                        item.Inspectors = data.Third;
                    }
                    //if (data != null)
                    //{
                    //    item.Operators = data.First;
                    //    item.MutualInspectors = data.Second;
                    //    item.Inspectors = data.Third;
                    //}
                }
            }

            return new PagedResultDto<GetJSDingLiangAndMultiMediaDto>(count, list);
        }
        /// <summary>
        /// 电连接器类多媒体
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<GetJSElectricalConnectorFormEntityDto>> GetJSElectricalConnectorFormList(GetFormInput input)
        {
            var a = await _formInfoRepository.FirstOrDefaultAsync(p => p.InstructionEntityId == input.Id && p.FormName == "电连接器类多媒体");
            var count = 0;
            var list = new List<GetJSElectricalConnectorFormEntityDto>();
            if (a != null)
            {
                var query = _electricalConnectorFormEntityRepository.GetAll().Where(p => p.FormInfoId == a.Id)
                .WhereIf(!input.Filter.IsNullOrWhiteSpace(),
                x => x.PartDrawNo.Contains(input.Filter)
                );
                count = await query.CountAsync();
                var instructionlist = await query.OrderBy(p => p.OrderNo).PageBy(input).ToListAsync();
                list = ObjectMapper.Map<List<GetJSElectricalConnectorFormEntityDto>>(instructionlist);
                foreach (var item in list)
                {
                    var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == item.Id);
                    item.FormInstructionRelation = ObjectMapper.Map<GetFormInstructionRelationDto>(formInstruction);

                    List<MediaFileList> mediaFileLists = new List<MediaFileList>();
                    if (item.MediaFileName != null && item.MediaFileName != "")
                    {
                        var fileNames = item.MediaFileName.Split(',');
                        foreach (var fileName in fileNames)
                        {
                            var types = fileName.Split('.');
                            MediaFileList mediaFileList = new MediaFileList();
                            mediaFileList.MediaFileName = fileName;
                            mediaFileList.Type = types[1];
                            mediaFileLists.Add(mediaFileList);

                        }
                    }
                    item.MediaFileLists = mediaFileLists;

                    item.Notekeepers = new List<UserSignatureInfoDto>();
                    item.Confirmers = new List<UserSignatureInfoDto>();
                    item.Inspectors = new List<UserSignatureInfoDto>();
                    var data = GetUserSignatureInfo(item.Notekeeper, item.Confirmer, item.Inspector);
                    if (data.First != null)
                    {
                        item.Notekeepers = data.First;
                    }
                    if (data.Second != null)
                    {
                        item.Confirmers = data.Second;
                    }
                    if (data.Third != null)
                    {
                        item.Inspectors = data.Third;
                    }

                }
            }

            return new PagedResultDto<GetJSElectricalConnectorFormEntityDto>(count, list);
        }
        public async Task<PagedResultDto<ProductInfosDto>> GetAllProductInfos(GetAllProductInfosInput input)
        {
            var productInfos = new List<ProductInfosDto>();

            var formInfos = _formInfoRepository.GetAll().Where(p => p.InstructionEntityId == input.Id);
            var count = 0;
            if (formInfos != null)
            {
                foreach (var item in formInfos)
                {
                    switch (item.FormName)
                    {
                        case "实物配套表":
                            var query = _materialObjectFormEntityRepository.GetAll().Where(p => p.FormInfoId == item.Id);
                            var materialObjectForms = ObjectMapper.Map<List<ProductInfosDto>>(query);
                            foreach (var list in materialObjectForms)
                            {
                                var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == list.Id);
                                list.FormInstructionRelation = ObjectMapper.Map<GetFormInstructionRelationDto>(formInstruction);
                                list.FormType = 1;
                                list.FromName = item.FormName;
                            }
                            productInfos.AddRange(materialObjectForms);
                            break;
                        case "定量检查表":
                            var dingLiangExaminationFormEntites = _dingLiangExaminationFormEntityRepository.GetAll().Where(p => p.FormInfoId == item.Id);
                            var dingLiangExaminationForms = ObjectMapper.Map<List<ProductInfosDto>>(dingLiangExaminationFormEntites);
                            foreach (var list in dingLiangExaminationForms)
                            {
                                var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == list.Id);
                                list.FormInstructionRelation = ObjectMapper.Map<GetFormInstructionRelationDto>(formInstruction);
                                list.FormType = 2;
                                list.FromName = item.FormName;
                            }
                            productInfos.AddRange(dingLiangExaminationForms);
                            break;
                        case "定性检查表":
                            var dingXingExaminationFormEntites = _dingXingExaminationFormEntityRepository.GetAll().Where(p => p.FormInfoId == item.Id);
                            var dingXingExaminationForms = ObjectMapper.Map<List<ProductInfosDto>>(dingXingExaminationFormEntites);
                            foreach (var list in dingXingExaminationForms)
                            {
                                var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == list.Id);
                                list.FormInstructionRelation = ObjectMapper.Map<GetFormInstructionRelationDto>(formInstruction);
                                list.FormType = 3;
                                list.FromName = item.FormName;
                            }
                            productInfos.AddRange(dingXingExaminationForms);
                            break;
                        case "多媒体检查表":
                            var multiMediaExaminationFormEntites = _multiMediaExaminationFormEntityRepository.GetAll().Where(p => p.FormInfoId == item.Id);
                            var multiMediaExaminationForms = ObjectMapper.Map<List<ProductInfosDto>>(multiMediaExaminationFormEntites);
                            foreach (var list in multiMediaExaminationForms)
                            {
                                var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == list.Id);
                                list.FormInstructionRelation = ObjectMapper.Map<GetFormInstructionRelationDto>(formInstruction);
                                list.FormType = 4;
                                list.FromName = item.FormName;
                                List<MediaFileList> mediaFileLists = new List<MediaFileList>();
                                if (list.MediaFileName != null && list.MediaFileName != "")
                                {
                                    var fileNames = list.MediaFileName.Split(',');
                                    foreach (var fileName in fileNames)
                                    {
                                        var types = fileName.Split('.');
                                        MediaFileList mediaFileList = new MediaFileList();
                                        mediaFileList.MediaFileName = fileName;
                                        mediaFileList.Type = types[1];
                                        mediaFileLists.Add(mediaFileList);

                                    }
                                }
                                list.MediaFileLists = mediaFileLists;
                            }
                            productInfos.AddRange(multiMediaExaminationForms);
                            break;
                        //带多媒体
                        case "定量&多媒体检查表":
                            var dingLiangAndMultiMediaExaminationFormEntites = _dingLiangAndMultiMediaExaminationFormEntityRepository.GetAll().Where(p => p.FormInfoId == item.Id);
                            var dingLiangAndMultiMediaExaminationForms = ObjectMapper.Map<List<ProductInfosDto>>(dingLiangAndMultiMediaExaminationFormEntites);
                            foreach (var list in dingLiangAndMultiMediaExaminationForms)
                            {
                                var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == list.Id);
                                list.FormInstructionRelation = ObjectMapper.Map<GetFormInstructionRelationDto>(formInstruction);
                                list.FormType = 5;
                                list.FromName = item.FormName;
                                List<MediaFileList> mediaFileLists = new List<MediaFileList>();
                                if (list.MediaFileName != null && list.MediaFileName != "")
                                {
                                    var fileNames = list.MediaFileName.Split(',');
                                    foreach (var fileName in fileNames)
                                    {
                                        var types = fileName.Split('.');
                                        MediaFileList mediaFileList = new MediaFileList();
                                        mediaFileList.MediaFileName = fileName;
                                        mediaFileList.Type = types[1];
                                        mediaFileLists.Add(mediaFileList);

                                    }
                                }
                                list.MediaFileLists = mediaFileLists;
                            }
                            productInfos.AddRange(dingLiangAndMultiMediaExaminationForms);
                            break;
                        case "力矩类定量检查表":
                            var momentRationExaminationFormEntites = _momentRationExaminationFormEntityRepository.GetAll().Where(p => p.FormInfoId == item.Id);
                            var momentRationExaminationForms = ObjectMapper.Map<List<ProductInfosDto>>(momentRationExaminationFormEntites);
                            foreach (var list in momentRationExaminationForms)
                            {
                                var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == list.Id);
                                list.FormInstructionRelation = ObjectMapper.Map<GetFormInstructionRelationDto>(formInstruction);
                                list.FormType = 6;
                                list.FromName = item.FormName;
                            }
                            productInfos.AddRange(momentRationExaminationForms);
                            break;
                        case "测试类定量检查表":
                            var testRationExaminationFormEntites = _testRationExaminationFormEntityRepository.GetAll().Where(p => p.FormInfoId == item.Id);
                            var testRationExaminationForms = ObjectMapper.Map<List<ProductInfosDto>>(testRationExaminationFormEntites);
                            foreach (var list in testRationExaminationForms)
                            {
                                var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == list.Id);
                                list.FormInstructionRelation = ObjectMapper.Map<GetFormInstructionRelationDto>(formInstruction);
                                list.FormType = 7;
                                list.FromName = item.FormName;
                            }
                            productInfos.AddRange(testRationExaminationForms);
                            break;

                        //带多媒体
                        case "电连接器类多媒体":
                            var electricalConnectorFormEntites = _electricalConnectorFormEntityRepository.GetAll().Where(p => p.FormInfoId == item.Id);
                            var electricalConnectorForms = ObjectMapper.Map<List<ProductInfosDto>>(electricalConnectorFormEntites);
                            foreach (var list in electricalConnectorForms)
                            {
                                var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == list.Id);
                                list.FormInstructionRelation = ObjectMapper.Map<GetFormInstructionRelationDto>(formInstruction);
                                list.FormType = 8;
                                list.FromName = item.FormName;
                                List<MediaFileList> mediaFileLists = new List<MediaFileList>();
                                if (list.MediaFileName != null && list.MediaFileName != "")
                                {
                                    var fileNames = list.MediaFileName.Split(',');
                                    foreach (var fileName in fileNames)
                                    {
                                        var types = fileName.Split('.');
                                        MediaFileList mediaFileList = new MediaFileList();
                                        mediaFileList.MediaFileName = fileName;
                                        mediaFileList.Type = types[1];
                                        mediaFileLists.Add(mediaFileList);

                                    }
                                }
                                list.MediaFileLists = mediaFileLists;
                            }
                            productInfos.AddRange(electricalConnectorForms);
                            break;
                        default:
                            break;
                    }
                }


                count = productInfos.Count;
                //productInfos = productInfos.OrderBy(p => p.FormType).ThenBy(c=>c.OrderNo)
                //    .PageBy(input)
                //    .ToList();
                productInfos = productInfos.AsQueryable<ProductInfosDto>().OrderBy(p => p.FormType).ThenBy(c => c.OrderNo).PageBy(input).ToList();
            }
            //       return productInfos;
            return new PagedResultDto<ProductInfosDto>(count, productInfos);
        }
        /// <summary>
        /// 指导书落实情况查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task TestDataCheck(Guid id)
        {
            var instruction = await _instructionEntityRepository.FirstOrDefaultAsync(c => c.Id == id); //获取对应的指导书
            var info = await _formInfoRepository.GetAllListAsync(c => c.InstructionEntityId == instruction.Id);//获取指导书的表数据

            var tableList = await GetTableBaseBy(instruction.Model, instruction.FaCi);//获取对用型号发次的所有工艺文件表
            foreach (var item in info)
            {
                var electricalConnectorList = await _electricalConnectorFormEntityRepository.GetAllListAsync(p => p.FormInfoId == item.Id); //电连接器类多媒体数据
                foreach (var electricalConnector in electricalConnectorList)
                {
                    //var formName = _formRelationEntityRepository.FirstOrDefault(x => x.TestProjectNum == electricalConnector.TestProjectNum).FormName; //根据对应关系获取要获得的表名
                    //var tt = tableList.Where(d => d.FormName == formName);//根据表名获取工艺文件对应数据
                    //var tc = tt.FirstOrDefault(a => a.FisrtDrawingNum == electricalConnector.ModeCode && a.SecondDrawingNum == electricalConnector.Name);//根据查询获取数据
                    //if (tc != null)
                    //{
                    //    var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == electricalConnector.Id);
                    //    formInstruction.IsImplement = 1;
                    //    formInstruction.ProcessesEntityId = tc.ProcessesEntityId;

                    //    formInstruction.WorkStepEntityId = tc.WorkStepEntityId;
                    //    var techniqueEntityId = _processesEntityRepository.FirstOrDefault(b => b.Id == tc.ProcessesEntityId).TechniqueEntityId;
                    //    var technique = _techniqueEntityRepository.FirstOrDefault(e => e.Id == techniqueEntityId);
                    //    formInstruction.TechniqueEntityId = technique.Id;
                    //    formInstruction.TechniqueName = technique.TechniqueName;
                    //    formInstruction.TechniqueNum = technique.TechniqueNumber;
                    //    _formInstructionRelationRepository.Update(formInstruction);
                    //}
                    await UpdateExaminationForm(tableList, electricalConnector.InspectionItemsNum, electricalConnector.PartDrawNo, electricalConnector.PartName, electricalConnector.ParamCode, electricalConnector.ProductNumber, electricalConnector.Id);

                }

                var dingLiangAndMultiMediaList = await _dingLiangAndMultiMediaExaminationFormEntityRepository.GetAllListAsync(p => p.FormInfoId == item.Id); //定量&多媒体检查表


                foreach (var dingLiangAndMultiMedia in dingLiangAndMultiMediaList)
                {

                    await UpdateExaminationForm(tableList, dingLiangAndMultiMedia.InspectionItemsNum, dingLiangAndMultiMedia.PartDrawNo, dingLiangAndMultiMedia.PartName, dingLiangAndMultiMedia.ParamCode, dingLiangAndMultiMedia.ProductNumber, dingLiangAndMultiMedia.Id);

                }

                var dingXingExaminationList = await _dingXingExaminationFormEntityRepository.GetAllListAsync(p => p.FormInfoId == item.Id);//定性检查表
                foreach (var dingXingExamination in dingXingExaminationList)
                {
                    await UpdateExaminationForm(tableList, dingXingExamination.InspectionItemsNum, dingXingExamination.PartDrawNo, dingXingExamination.PartName, dingXingExamination.ParamCode, dingXingExamination.ProductNumber, dingXingExamination.Id);
                }

                var dingLiangExaminationList = await _dingLiangExaminationFormEntityRepository.GetAllListAsync(p => p.FormInfoId == item.Id);
                foreach (var dingLiangExaminatin in dingLiangExaminationList)
                {
                    await UpdateExaminationForm(tableList, dingLiangExaminatin.InspectionItemsNum, dingLiangExaminatin.PartDrawNo, dingLiangExaminatin.PartName, dingLiangExaminatin.ParamCode, dingLiangExaminatin.ProductNumber, dingLiangExaminatin.Id);
                }

                var momentRationExaminationList = await _momentRationExaminationFormEntityRepository.GetAllListAsync(p => p.FormInfoId == item.Id);//  力矩类定量检查表
                foreach (var momentRationExamination in momentRationExaminationList)
                {
                    await UpdateExaminationForm(tableList, momentRationExamination.InspectionItemsNum, momentRationExamination.PartDrawNo, momentRationExamination.PartName, momentRationExamination.ParamCode, momentRationExamination.ProductNumber, momentRationExamination.Id);
                }

                var testRationExaminationList = await _testRationExaminationFormEntityRepository.GetAllListAsync(p => p.FormInfoId == item.Id);//测试类定量检查表

                foreach (var testRationExamination in testRationExaminationList)
                {
                    await UpdateExaminationForm(tableList, testRationExamination.InspectionItemsNum, testRationExamination.PartDrawNo, testRationExamination.PartName, testRationExamination.ParamCode, testRationExamination.ProductNumber, testRationExamination.Id);
                }


                var multiMediaExaminationList = await _multiMediaExaminationFormEntityRepository.GetAllListAsync(p => p.FormInfoId == item.Id);//多媒体检查表
                foreach (var multiMediaExamination in multiMediaExaminationList)
                {
                    await UpdateExaminationForm(tableList, multiMediaExamination.InspectionItemsNum, multiMediaExamination.PartDrawNo, multiMediaExamination.PartName, multiMediaExamination.ParamCode, multiMediaExamination.ProductNumber, multiMediaExamination.Id);
                }

            }

        }


        private static GetUserSignatureInfoDto GetUserSignatureInfo(string Operator, string MutualInspector, string Inspector)
        {
            var data = new GetUserSignatureInfoDto();
            if (Operator != null && Operator != "")
            {
                List<UserSignatureInfoDto> operators = new List<UserSignatureInfoDto>();
                var operatorsList = JsonConvert.DeserializeObject<List<UserJsonDto>>(Operator.Replace(" ", ""));
                foreach (var operatorer in operatorsList)
                {
                    UserSignatureInfoDto user = new UserSignatureInfoDto();
                    user.UserSignature = operatorer.Name;
                    operators.Add(user);

                }
                data.First = operators;
            }
            if (MutualInspector != null && MutualInspector != "")
            {
                List<UserSignatureInfoDto> mutualInspectors = new List<UserSignatureInfoDto>();
                //List<string> mutualInspectors = new List<string>();
                var mutualInspectorsList = JsonConvert.DeserializeObject<List<UserJsonDto>>(MutualInspector.Replace(" ", ""));
                foreach (var mutualInspectorer in mutualInspectorsList)
                {
                    UserSignatureInfoDto user = new UserSignatureInfoDto();
                    user.UserSignature = mutualInspectorer.Name;
                    mutualInspectors.Add(user);

                }
                data.Second = mutualInspectors;
            }
            if (Inspector != null && Inspector != "")
            {
                List<UserSignatureInfoDto> inspectors = new List<UserSignatureInfoDto>();
                var inspectorsList = JsonConvert.DeserializeObject<List<UserJsonDto>>(Inspector.Replace(" ", ""));
                foreach (var inspectorer in inspectorsList)
                {
                    UserSignatureInfoDto user = new UserSignatureInfoDto();
                    user.UserSignature = inspectorer.Name;
                    inspectors.Add(user);

                }
                data.Third = inspectors;
            }
            return data;
        }


        /// <summary>
        /// 数据赋值
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task TestDataAssignment(Guid id)
        {
            var instruction = await _instructionEntityRepository.FirstOrDefaultAsync(c => c.Id == id); //获取对应的指导书
            var info = await _formInfoRepository.GetAllListAsync(c => c.InstructionEntityId == instruction.Id);//获取指导书的表数据

            var tableList = await GetRTableBase(instruction.Model, instruction.FaCi);//获取对用型号发次的所有工艺文件表
            foreach (var item in info)
            {
                switch (item.FormName)
                {
                    case "实物配套表": break;
                    case "定量检查表":
                        var dingLiangExaminationList = await _dingLiangExaminationFormEntityRepository.GetAllListAsync(p => p.FormInfoId == item.Id);
                        foreach (var dingLiangExaminatin in dingLiangExaminationList)
                        {
                            var form = await _formRelationEntityRepository.FirstOrDefaultAsync(x => x.TestProjectNum == dingLiangExaminatin.InspectionItemsNum);
                            if (form != null)
                            {
                                var tt = tableList.Where(d => d.FormName == form.FormName);//根据表名获取工艺文件对应数据

                                var tc = tt.WhereIf(!dingLiangExaminatin.PartDrawNo.IsNullOrEmpty(), t => t.FisrtDrawingNum.Equals(dingLiangExaminatin.PartDrawNo))
                                   .WhereIf(!dingLiangExaminatin.PartName.IsNullOrEmpty(), t => t.FirstDrawingName.Equals(dingLiangExaminatin.PartName))
                                    .WhereIf(!dingLiangExaminatin.ParamCode.IsNullOrEmpty(), t => t.ParamCode.Equals(dingLiangExaminatin.ParamCode))
                                      .WhereIf(!dingLiangExaminatin.ProductNumber.IsNullOrEmpty(), t => t.ProductNum.Equals(dingLiangExaminatin.ProductNumber))
                                   .FirstOrDefault();

                                var im = ObjectMapper.Map<RTableBasesOutDto>(tc);
                                if (tc != null)
                                {

                                    var FormCols = await _formColRelationEntityRepository.GetAllListAsync(c => c.FormRelationEntityId == form.Id);

                                    foreach (var FormCol in FormCols)
                                    {
                                        //var aa = im.GetType().GetProperty(FormCol.TechniqueFormColName).Name ;

                                        var outName = FormCol.TechniqueFormColName.Substring(0, 1).ToUpper() + FormCol.TechniqueFormColName.Substring(1);

                                        var b = im.GetType().GetProperty(outName).GetValue(im, null).ToString();

                                        var inName = FormCol.ColName.Substring(0, 1).ToUpper() + FormCol.ColName.Substring(1);
                                        PropertyInfo pi = dingLiangExaminatin.GetType().GetProperty(inName);
                                        if (pi != null)
                                        {
                                            var d = GetObject(pi, b);
                                            pi.SetValue(dingLiangExaminatin, d);
                                            if (dingLiangExaminatin.Operator != "" || dingLiangExaminatin.MutualInspector != "" || dingLiangExaminatin.Inspector != "")
                                            {
                                                GetInspectionSignatureInfo(dingLiangExaminatin.Id, dingLiangExaminatin.Operator, dingLiangExaminatin.MutualInspector, dingLiangExaminatin.Inspector, instruction.FileNum);
                                            }
                                        }


                                    }
                                    var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == dingLiangExaminatin.Id);
                                    var rprocesses = _rprocessesEntityRepository.FirstOrDefault(c => c.Id == im.RProcessesEntityId);
                                    var rtech = _rtechniqueEntityRepository.FirstOrDefault(c => c.Id == rprocesses.RTechniqueEntityId);
                                    formInstruction.IsFinsh = 1;
                                    formInstruction.RProcessesEntityId = im.RProcessesEntityId;
                                    formInstruction.RProcessesEntityName = "工序" + rprocesses.ProcessesSequence.ToString() + ":" + rprocesses.ProcessesName;
                                    formInstruction.RTechniqueEntityId = rtech.Id;
                                    formInstruction.RTechniqueName = rtech.ModuleName;
                                    formInstruction.WorkStepEntityId = im.RWorkStepEntityId;
                                    _formInstructionRelationRepository.Update(formInstruction);

                                }
                            }


                        }
                        break;
                    case "定性检查表":
                        var dingXingExaminationList = await _dingXingExaminationFormEntityRepository.GetAllListAsync(p => p.FormInfoId == item.Id);//定性检查表
                        foreach (var dingXingExamination in dingXingExaminationList)
                        {
                            var form = await _formRelationEntityRepository.FirstOrDefaultAsync(x => x.TestProjectNum == dingXingExamination.InspectionItemsNum);
                            if (form != null)
                            {
                                var tt = tableList.Where(d => d.FormName == form.FormName);//根据表名获取工艺文件对应数据

                                var tc = tt.WhereIf(!dingXingExamination.PartDrawNo.IsNullOrEmpty(), t => t.FisrtDrawingNum.Equals(dingXingExamination.PartDrawNo))
                                   .WhereIf(!dingXingExamination.PartName.IsNullOrEmpty(), t => t.FirstDrawingName.Equals(dingXingExamination.PartName))
                                    .WhereIf(!dingXingExamination.ParamCode.IsNullOrEmpty(), t => t.ParamCode.Equals(dingXingExamination.ParamCode))
                                      .WhereIf(!dingXingExamination.ProductNumber.IsNullOrEmpty(), t => t.ProductNum.Equals(dingXingExamination.ProductNumber))
                                   .FirstOrDefault();

                                var im = ObjectMapper.Map<RTableBasesOutDto>(tc);
                                if (tc != null)
                                {

                                    var FormCols = await _formColRelationEntityRepository.GetAllListAsync(c => c.FormRelationEntityId == form.Id);

                                    foreach (var FormCol in FormCols)
                                    {
                                        //var aa = im.GetType().GetProperty(FormCol.TechniqueFormColName).Name ;

                                        var outName = FormCol.TechniqueFormColName.Substring(0, 1).ToUpper() + FormCol.TechniqueFormColName.Substring(1);

                                        var b = im.GetType().GetProperty(outName).GetValue(im, null).ToString();

                                        var inName = FormCol.ColName.Substring(0, 1).ToUpper() + FormCol.ColName.Substring(1);
                                        PropertyInfo pi = dingXingExamination.GetType().GetProperty(inName);
                                        if (pi != null)
                                        {
                                            var d = GetObject(pi, b);
                                            pi.SetValue(dingXingExamination, d);
                                            if (dingXingExamination.Operator != "" || dingXingExamination.MutualInspector != "" || dingXingExamination.Inspector != "")
                                            {
                                                GetInspectionSignatureInfo(dingXingExamination.Id, dingXingExamination.Operator, dingXingExamination.MutualInspector, dingXingExamination.Inspector, instruction.FileNum);
                                            }
                                        }

                                    }
                                    var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == dingXingExamination.Id);
                                    var rprocesses = _rprocessesEntityRepository.FirstOrDefault(c => c.Id == im.RProcessesEntityId);
                                    var rtech = _rtechniqueEntityRepository.FirstOrDefault(c => c.Id == rprocesses.RTechniqueEntityId);
                                    formInstruction.IsFinsh = 1;
                                    formInstruction.RProcessesEntityId = im.RProcessesEntityId;
                                    formInstruction.RProcessesEntityName = "工序" + rprocesses.ProcessesSequence.ToString() + ":" + rprocesses.ProcessesName;
                                    formInstruction.RTechniqueEntityId = rtech.Id;
                                    formInstruction.RTechniqueName = rtech.ModuleName;
                                    formInstruction.WorkStepEntityId = im.RWorkStepEntityId;
                                    _formInstructionRelationRepository.Update(formInstruction);
                                }
                            }
                        }
                        break;
                    case "多媒体检查表":
                        var multiMediaExaminationList = await _multiMediaExaminationFormEntityRepository.GetAllListAsync(p => p.FormInfoId == item.Id);//多媒体检查表
                        foreach (var multiMediaExamination in multiMediaExaminationList)
                        {
                            var form = await _formRelationEntityRepository.FirstOrDefaultAsync(x => x.TestProjectNum == multiMediaExamination.InspectionItemsNum);
                            if (form != null)
                            {
                                var tt = tableList.Where(d => d.FormName == form.FormName);//根据表名获取工艺文件对应数据

                                var tc = tt.WhereIf(!multiMediaExamination.PartDrawNo.IsNullOrEmpty(), t => t.FisrtDrawingNum.Equals(multiMediaExamination.PartDrawNo))
                                   .WhereIf(!multiMediaExamination.PartName.IsNullOrEmpty(), t => t.FirstDrawingName.Equals(multiMediaExamination.PartName))
                                    .WhereIf(!multiMediaExamination.ParamCode.IsNullOrEmpty(), t => t.ParamCode.Equals(multiMediaExamination.ParamCode))
                                      .WhereIf(!multiMediaExamination.ProductNumber.IsNullOrEmpty(), t => t.ProductNum.Equals(multiMediaExamination.ProductNumber))
                                   .FirstOrDefault();

                                var im = ObjectMapper.Map<RTableBasesOutDto>(tc);
                                if (tc != null)
                                {

                                    var FormCols = await _formColRelationEntityRepository.GetAllListAsync(c => c.FormRelationEntityId == form.Id);

                                    foreach (var FormCol in FormCols)
                                    {
                                        //var aa = im.GetType().GetProperty(FormCol.TechniqueFormColName).Name ;

                                        var outName = FormCol.TechniqueFormColName.Substring(0, 1).ToUpper() + FormCol.TechniqueFormColName.Substring(1);

                                        var b = im.GetType().GetProperty(outName).GetValue(im, null).ToString();

                                        var inName = FormCol.ColName.Substring(0, 1).ToUpper() + FormCol.ColName.Substring(1);
                                        PropertyInfo pi = multiMediaExamination.GetType().GetProperty(inName);
                                        if (pi != null)
                                        {
                                            var d = GetObject(pi, b);
                                            pi.SetValue(multiMediaExamination, d);

                                            if (multiMediaExamination.MediaFileName != "") { GetMeasuredValue(multiMediaExamination.Id, multiMediaExamination.MediaFileName, instruction.FileNum); }

                                            if (multiMediaExamination.Operator != "" || multiMediaExamination.MutualInspector != "" || multiMediaExamination.Confirmer != "") { GetInspectionSignatureInfo(multiMediaExamination.Id, multiMediaExamination.Operator, multiMediaExamination.Confirmer, multiMediaExamination.MutualInspector, instruction.FileNum); }
                                        }
                                    }
                                    var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == multiMediaExamination.Id);
                                    var rprocesses = _rprocessesEntityRepository.FirstOrDefault(c => c.Id == im.RProcessesEntityId);
                                    var rtech = _rtechniqueEntityRepository.FirstOrDefault(c => c.Id == rprocesses.RTechniqueEntityId);
                                    formInstruction.IsFinsh = 1;
                                    formInstruction.RProcessesEntityId = im.RProcessesEntityId;
                                    formInstruction.RProcessesEntityName = "工序" + rprocesses.ProcessesSequence.ToString() + ":" + rprocesses.ProcessesName;
                                    formInstruction.RTechniqueEntityId = rtech.Id;
                                    formInstruction.RTechniqueName = rtech.ModuleName;
                                    formInstruction.WorkStepEntityId = im.RWorkStepEntityId;
                                    _formInstructionRelationRepository.Update(formInstruction);
                                }
                            }
                        }
                        break;
                    case "定量&多媒体检查表":
                        var dingLiangAndMultiMediaList = await _dingLiangAndMultiMediaExaminationFormEntityRepository.GetAllListAsync(p => p.FormInfoId == item.Id); //定量&多媒体检查表
                        foreach (var dingLiangAndMultiMedia in dingLiangAndMultiMediaList)
                        {
                            var form = await _formRelationEntityRepository.FirstOrDefaultAsync(x => x.TestProjectNum == dingLiangAndMultiMedia.InspectionItemsNum);
                            if (form != null)
                            {
                                var tt = tableList.Where(d => d.FormName == form.FormName);//根据表名获取工艺文件对应数据

                                var tc = tt.WhereIf(!dingLiangAndMultiMedia.PartDrawNo.IsNullOrEmpty(), t => t.FisrtDrawingNum.Equals(dingLiangAndMultiMedia.PartDrawNo))
                                   .WhereIf(!dingLiangAndMultiMedia.PartName.IsNullOrEmpty(), t => t.FirstDrawingName.Equals(dingLiangAndMultiMedia.PartName))
                                    .WhereIf(!dingLiangAndMultiMedia.ParamCode.IsNullOrEmpty(), t => t.ParamCode.Equals(dingLiangAndMultiMedia.ParamCode))
                                      .WhereIf(!dingLiangAndMultiMedia.ProductNumber.IsNullOrEmpty(), t => t.ProductNum.Equals(dingLiangAndMultiMedia.ProductNumber))
                                   .FirstOrDefault();
                                var im = ObjectMapper.Map<RTableBasesOutDto>(tc);

                                if (tc != null)
                                {

                                    var FormCols = await _formColRelationEntityRepository.GetAllListAsync(c => c.FormRelationEntityId == form.Id);

                                    foreach (var FormCol in FormCols)
                                    {
                                        //var aa = im.GetType().GetProperty(FormCol.TechniqueFormColName).Name ;

                                        var outName = FormCol.TechniqueFormColName.Substring(0, 1).ToUpper() + FormCol.TechniqueFormColName.Substring(1);

                                        var b = im.GetType().GetProperty(outName).GetValue(im, null).ToString();

                                        var inName = FormCol.ColName.Substring(0, 1).ToUpper() + FormCol.ColName.Substring(1);
                                        PropertyInfo pi = dingLiangAndMultiMedia.GetType().GetProperty(inName);
                                        if (pi != null)
                                        {
                                            var d = GetObject(pi, b);
                                            pi.SetValue(dingLiangAndMultiMedia, d);
                                            if (dingLiangAndMultiMedia.MediaFileName != "")
                                            {
                                                GetMeasuredValue(dingLiangAndMultiMedia.Id, dingLiangAndMultiMedia.MediaFileName, instruction.FileNum);
                                            }

                                            if (dingLiangAndMultiMedia.Operator != "" || dingLiangAndMultiMedia.MutualInspector != "" || dingLiangAndMultiMedia.Inspector != "")
                                            {
                                                GetInspectionSignatureInfo(dingLiangAndMultiMedia.Id, dingLiangAndMultiMedia.Operator, dingLiangAndMultiMedia.Inspector, dingLiangAndMultiMedia.MutualInspector, instruction.FileNum);
                                            }
                                        }

                                    }

                                    var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == dingLiangAndMultiMedia.Id);
                                    var rprocesses = _rprocessesEntityRepository.FirstOrDefault(c => c.Id == im.RProcessesEntityId);
                                    var rtech = _rtechniqueEntityRepository.FirstOrDefault(c => c.Id == rprocesses.RTechniqueEntityId);
                                    formInstruction.IsFinsh = 1;
                                    formInstruction.RProcessesEntityId = im.RProcessesEntityId;
                                    formInstruction.RProcessesEntityName = "工序" + rprocesses.ProcessesSequence.ToString() + ":" + rprocesses.ProcessesName;
                                    formInstruction.RTechniqueEntityId = rtech.Id;
                                    formInstruction.RTechniqueName = rtech.ModuleName;
                                    formInstruction.WorkStepEntityId = im.RWorkStepEntityId;
                                    _formInstructionRelationRepository.Update(formInstruction);
                                }

                            }

                        }
                        break;
                    case "力矩类定量检查表":
                        var momentRationExaminationList = await _momentRationExaminationFormEntityRepository.GetAllListAsync(p => p.FormInfoId == item.Id);//  力矩类定量检查表
                        foreach (var momentRationExamination in momentRationExaminationList)
                        {


                            var form = await _formRelationEntityRepository.FirstOrDefaultAsync(x => x.TestProjectNum == momentRationExamination.InspectionItemsNum);
                            if (form != null)
                            {
                                var tt = tableList.Where(d => d.FormName == form.FormName);//根据表名获取工艺文件对应数据

                                var tc = tt.WhereIf(!momentRationExamination.PartDrawNo.IsNullOrEmpty(), t => t.FisrtDrawingNum.Equals(momentRationExamination.PartDrawNo))
                                   .WhereIf(!momentRationExamination.PartName.IsNullOrEmpty(), t => t.FirstDrawingName.Equals(momentRationExamination.PartName))
                                    .WhereIf(!momentRationExamination.ParamCode.IsNullOrEmpty(), t => t.ParamCode.Equals(momentRationExamination.ParamCode))
                                      .WhereIf(!momentRationExamination.ProductNumber.IsNullOrEmpty(), t => t.ProductNum.Equals(momentRationExamination.ProductNumber))
                                   .FirstOrDefault();

                                var im = ObjectMapper.Map<RTableBasesOutDto>(tc);
                                if (tc != null)
                                {

                                    var FormCols = await _formColRelationEntityRepository.GetAllListAsync(c => c.FormRelationEntityId == form.Id);

                                    foreach (var FormCol in FormCols)
                                    {
                                        //var aa = im.GetType().GetProperty(FormCol.TechniqueFormColName).Name ;

                                        var outName = FormCol.TechniqueFormColName.Substring(0, 1).ToUpper() + FormCol.TechniqueFormColName.Substring(1);

                                        var b = im.GetType().GetProperty(outName).GetValue(im, null).ToString();

                                        var inName = FormCol.ColName.Substring(0, 1).ToUpper() + FormCol.ColName.Substring(1);
                                        PropertyInfo pi = momentRationExamination.GetType().GetProperty(inName);
                                        if (pi != null)
                                        {
                                            var d = GetObject(pi, b);
                                            pi.SetValue(momentRationExamination, d);
                                            if (momentRationExamination.Operator != "" || momentRationExamination.MutualInspector != "" || momentRationExamination.Inspector != "")
                                            {
                                                GetInspectionSignatureInfo(momentRationExamination.Id, momentRationExamination.Operator, momentRationExamination.Inspector, momentRationExamination.MutualInspector, instruction.FileNum);
                                            }
                                        }
                                    }
                                    var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == momentRationExamination.Id);
                                    var rprocesses = _rprocessesEntityRepository.FirstOrDefault(c => c.Id == im.RProcessesEntityId);
                                    var rtech = _rtechniqueEntityRepository.FirstOrDefault(c => c.Id == rprocesses.RTechniqueEntityId);
                                    formInstruction.IsFinsh = 1;
                                    formInstruction.RProcessesEntityId = im.RProcessesEntityId;
                                    formInstruction.RProcessesEntityName = "工序" + rprocesses.ProcessesSequence.ToString() + ":" + rprocesses.ProcessesName;
                                    formInstruction.RTechniqueEntityId = rtech.Id;
                                    formInstruction.RTechniqueName = rtech.ModuleName;
                                    formInstruction.WorkStepEntityId = im.RWorkStepEntityId;
                                    _formInstructionRelationRepository.Update(formInstruction);

                                }

                            }
                        }
                        break;
                    case "测试类定量检查表":
                        var testRationExaminationList = await _testRationExaminationFormEntityRepository.GetAllListAsync(p => p.FormInfoId == item.Id);//测试类定量检查表
                        foreach (var testRationExamination in testRationExaminationList)
                        {
                            var form = await _formRelationEntityRepository.FirstOrDefaultAsync(x => x.TestProjectNum == testRationExamination.InspectionItemsNum);
                            if (form != null)
                            {
                                var tt = tableList.Where(d => d.FormName == form.FormName);//根据表名获取工艺文件对应数据

                                var tc = tt.WhereIf(!testRationExamination.PartDrawNo.IsNullOrEmpty(), t => t.FisrtDrawingNum.Equals(testRationExamination.PartDrawNo))
                                   .WhereIf(!testRationExamination.PartName.IsNullOrEmpty(), t => t.FirstDrawingName.Equals(testRationExamination.PartName))
                                    .WhereIf(!testRationExamination.ParamCode.IsNullOrEmpty(), t => t.ParamCode.Equals(testRationExamination.ParamCode))
                                      .WhereIf(!testRationExamination.ProductNumber.IsNullOrEmpty(), t => t.ProductNum.Equals(testRationExamination.ProductNumber))
                                   .FirstOrDefault();

                                var im = ObjectMapper.Map<RTableBasesOutDto>(tc);
                                if (tc != null)
                                {

                                    var FormCols = await _formColRelationEntityRepository.GetAllListAsync(c => c.FormRelationEntityId == form.Id);

                                    foreach (var FormCol in FormCols)
                                    {
                                        //var aa = im.GetType().GetProperty(FormCol.TechniqueFormColName).Name ;

                                        var outName = FormCol.TechniqueFormColName.Substring(0, 1).ToUpper() + FormCol.TechniqueFormColName.Substring(1);

                                        var b = im.GetType().GetProperty(outName).GetValue(im, null).ToString();

                                        var inName = FormCol.ColName.Substring(0, 1).ToUpper() + FormCol.ColName.Substring(1);
                                        PropertyInfo pi = testRationExamination.GetType().GetProperty(inName);
                                        if (pi != null)
                                        {
                                            var d = GetObject(pi, b);
                                            pi.SetValue(testRationExamination, d);

                                            if (testRationExamination.Operator != "" || testRationExamination.MutualInspector != "" || testRationExamination.Inspector != "")
                                            {
                                                GetInspectionSignatureInfo(testRationExamination.Id, testRationExamination.Operator, testRationExamination.Inspector, testRationExamination.MutualInspector, instruction.FileNum);
                                            }
                                        }
                                    }
                                    var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == testRationExamination.Id);
                                    var rprocesses = _rprocessesEntityRepository.FirstOrDefault(c => c.Id == im.RProcessesEntityId);
                                    var rtech = _rtechniqueEntityRepository.FirstOrDefault(c => c.Id == rprocesses.RTechniqueEntityId);
                                    formInstruction.IsFinsh = 1;
                                    formInstruction.RProcessesEntityId = im.RProcessesEntityId;
                                    formInstruction.RProcessesEntityName = "工序" + rprocesses.ProcessesSequence.ToString() + ":" + rprocesses.ProcessesName;
                                    formInstruction.RTechniqueEntityId = rtech.Id;
                                    formInstruction.RTechniqueName = rtech.ModuleName;
                                    formInstruction.WorkStepEntityId = im.RWorkStepEntityId;
                                    _formInstructionRelationRepository.Update(formInstruction);

                                }
                            }
                        }
                        break;
                    case "电连接器类多媒体":
                        var electricalConnectorList = await _electricalConnectorFormEntityRepository.GetAllListAsync(p => p.FormInfoId == item.Id); //电连接器类多媒体数据
                        foreach (var electricalConnector in electricalConnectorList)
                        {

                            var form = await _formRelationEntityRepository.FirstOrDefaultAsync(x => x.TestProjectNum == electricalConnector.InspectionItemsNum);
                            if (form != null)
                            {
                                var tt = tableList.Where(d => d.FormName == form.FormName);//根据表名获取工艺文件对应数据

                                var tc = tt.WhereIf(!electricalConnector.PartDrawNo.IsNullOrEmpty(), t => t.FisrtDrawingNum.Equals(electricalConnector.PartDrawNo))
                                   .WhereIf(!electricalConnector.PartName.IsNullOrEmpty(), t => t.FirstDrawingName.Equals(electricalConnector.PartName))
                                    .WhereIf(!electricalConnector.ParamCode.IsNullOrEmpty(), t => t.ParamCode.Equals(electricalConnector.ParamCode))
                                      .WhereIf(!electricalConnector.ProductNumber.IsNullOrEmpty(), t => t.ProductNum.Equals(electricalConnector.ProductNumber))
                                   .FirstOrDefault();

                                var im = ObjectMapper.Map<RTableBasesOutDto>(tc);
                                if (tc != null)
                                {

                                    var FormCols = await _formColRelationEntityRepository.GetAllListAsync(c => c.FormRelationEntityId == form.Id);

                                    foreach (var FormCol in FormCols)
                                    {
                                        //var aa = im.GetType().GetProperty(FormCol.TechniqueFormColName).Name ;

                                        var outName = FormCol.TechniqueFormColName.Substring(0, 1).ToUpper() + FormCol.TechniqueFormColName.Substring(1);

                                        var b = im.GetType().GetProperty(outName).GetValue(im, null).ToString();

                                        var inName = FormCol.ColName.Substring(0, 1).ToUpper() + FormCol.ColName.Substring(1);
                                        PropertyInfo pi = electricalConnector.GetType().GetProperty(inName);
                                        if (pi != null)
                                        {
                                            var d = GetObject(pi, b);
                                            pi.SetValue(electricalConnector, d);

                                            if (electricalConnector.MediaFileName != "")
                                            {
                                                GetMeasuredValue(electricalConnector.Id, electricalConnector.MediaFileName, instruction.FileNum);
                                            }
                                            if (electricalConnector.Notekeeper != "" && electricalConnector.Notekeeper != null || electricalConnector.Confirmer != "" && electricalConnector.Confirmer != null || electricalConnector.Inspector != "" && electricalConnector.Inspector != null)
                                            {

                                                GetInspectionSignatureInfo(electricalConnector.Id, electricalConnector.Notekeeper, electricalConnector.Inspector, electricalConnector.Confirmer, instruction.FileNum);
                                            }
                                        }

                                    }
                                    var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == electricalConnector.Id);
                                    var rprocesses = _rprocessesEntityRepository.FirstOrDefault(c => c.Id == im.RProcessesEntityId);
                                    var rtech = _rtechniqueEntityRepository.FirstOrDefault(c => c.Id == rprocesses.RTechniqueEntityId);
                                    formInstruction.IsFinsh = 1;
                                    formInstruction.RProcessesEntityId = im.RProcessesEntityId;
                                    formInstruction.RProcessesEntityName = "工序" + rprocesses.ProcessesSequence.ToString() + ":" + rprocesses.ProcessesName;
                                    formInstruction.RTechniqueEntityId = rtech.Id;
                                    formInstruction.RTechniqueName = rtech.ModuleName;
                                    formInstruction.WorkStepEntityId = im.RWorkStepEntityId;
                                    _formInstructionRelationRepository.Update(formInstruction);
                                }
                            }
                        }
                        break;
                    default:
                        break;
                }

            }

        }

        private async Task UpdateExaminationForm(List<TableBase> tableList, string inspectionItemsNum, string partDrawNo, string partName, string paramCode, string productNumber, Guid id)
        {



            var form = _formRelationEntityRepository.FirstOrDefault(x => x.TestProjectNum == inspectionItemsNum);
            if (form != null)
            {
                var formName = form.FormName;
                //根据对应关系获取要获得的表名
                var tt = tableList.Where(d => d.FormName == formName);//根据表名获取工艺文件对应数据
                                                                      // var tc = tt.FirstOrDefault(a => a.FisrtDrawingNum == PartDrawNo && a.SecondDrawingNum == name);//根据查询获取数据

                // var tc = tt.FirstOrDefault(a => a.FirstDrawingName.Equals(partName) && a.FisrtDrawingNum.Equals(partDrawNo) && a.ProductNum.Equals(productNumber) && a.ParamCode.Equals(paramCode));


                var tc = tt.WhereIf(!partDrawNo.IsNullOrEmpty(), t => t.FisrtDrawingNum == partDrawNo)
                   .WhereIf(!partName.IsNullOrEmpty(), t => t.FirstDrawingName == (partName))
                    .WhereIf(!paramCode.IsNullOrEmpty(), t => t.ParamCode == (paramCode))
                      .WhereIf(!productNumber.IsNullOrEmpty(), t => t.ProductNum == (productNumber))
                   .FirstOrDefault();


                if (tc != null)
                {
                    var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == id);
                    formInstruction.IsImplement = 1;
                    formInstruction.ProcessesEntityId = tc.ProcessesEntityId;

                    formInstruction.WorkStepEntityId = tc.WorkStepEntityId;
                    var processes = _processesEntityRepository.FirstOrDefault(b => b.Id == tc.ProcessesEntityId);
                    var technique = _techniqueEntityRepository.FirstOrDefault(e => e.Id == processes.TechniqueEntityId);
                    formInstruction.TechniqueEntityId = technique.Id;
                    formInstruction.TechniqueName = technique.ModuleName;//修改显示的工艺文件的名称
                    formInstruction.TechniqueNum = technique.TechniqueNumber;
                    formInstruction.ProcessesEntityId = processes.Id;
                    formInstruction.ProcessesEntityName = "工序" + processes.ProcessesSequence.ToString() + ":" + processes.ProcessesName;
                    _formInstructionRelationRepository.Update(formInstruction);
                }

            }
            //else
            //{
            //    throw new UserFriendlyException("无对应关系 ");
            //}

        }



        public async Task<List<TableBase>> GetTableBaseBy(string model, string lotNo)
        {
            var query = from temp1 in _bopNodeRepository.GetAll()
                        join temp2 in _bopDescRepository.GetAll() on temp1.BopDescId equals temp2.Id
                        where temp1.TechniqueEntityId != null
                        select new { Model = temp2.Model, LotNo = temp2.LotNo, DrawingNo = temp2.DrawingNo, TechniqueEntityId = temp1.TechniqueEntityId };
            var techList = query.Where(c => c.LotNo == lotNo && c.Model == model)
                .Select(d => d.TechniqueEntityId);

            var result = _techniqueEntityRepository.GetAllIncluding(c => c.ProcessesEntities).Where(d => techList.Contains(d.Id)).ToList();

            var tt = ObjectMapper.Map<List<TechniqueDto>>(result);

            var pr = new List<Guid>();
            foreach (var item in tt)
            {
                foreach (var p in item.ProcessesListDto)
                {
                    pr.Add(p.Id);
                }
            };
            var tables = new List<TableBase>();
            foreach (var item in pr)
            {
                var table = _tableBaseRepository.GetAll().Where(c => c.ProcessesEntityId == item).ToList();
                tables.AddRange(table);
            }



            return tables;

        }

        public async Task<List<RTableBase>> GetRTableBase(string model, string lotNo)
        {
            var query = from temp1 in _rbopNodeRepository.GetAll()
                        join temp2 in _rbopDescRepository.GetAll() on temp1.RbopDescId equals temp2.Id
                        where temp1.TechniqueEntityId != null
                        select new { Model = temp2.Model, LotNo = temp2.LotNo, DrawingNo = temp2.DrawingNo, TechniqueEntityId = temp1.TechniqueEntityId };
            var techList = query.Where(c => c.LotNo == lotNo && c.Model == model)
                .Select(d => d.TechniqueEntityId);

            var result = _rtechniqueEntityRepository.GetAllIncluding(c => c.ProcessesEntities).Where(d => techList.Contains(d.TechniqueEntityId)).ToList();


            var tt = ObjectMapper.Map<List<TechniqueReturnDto>>(result);

            var pr = new List<Guid>();
            foreach (var item in tt)
            {
                foreach (var p in item.ProcessesListDto)
                {
                    pr.Add(p.Id);
                }
            };
            var tables = new List<RTableBase>();
            foreach (var item in pr)
            {
                var table = _rtableBaseRepository.GetAllIncluding(a => a.InspectionSignature, b => b.MeasuredResult, i => i.MultiMedia, l => l.Environment, e => e.RecordRequirements).Where(c => c.RProcessesEntityId == item).ToList();
                tables.AddRange(table);
            }



            return tables;
        }


        private static string GetFieldName(object entity, string values)
        {
            var cccc = entity.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy);
            var names = "";


            foreach (var item in cccc)
            {
                string name = item.Name; //名称
                object value = item.GetValue(values);  //值


                if (value != null)
                {
                    if (value.ToString() == value)
                    {
                        names = name;
                    }
                }

            }

            return names;
        }

        /// <summary>
        /// 转换值的类型
        /// </summary>
        /// <param name="p"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static object GetObject(PropertyInfo p, string value)
        {
            var type = p.PropertyType.Name.ToString().ToLower();
            if (type == "nullable`1")
            {
                System.ComponentModel.NullableConverter nullableConverter =

           new System.ComponentModel.NullableConverter(p.PropertyType);

                type = nullableConverter.UnderlyingType.Name.ToString().ToLower();
            }


            switch (type)
            {
                case "int16":
                    return Convert.ToInt16(value);
                case "int32":
                    return Convert.ToInt32(value);
                case "int64":
                    return Convert.ToInt64(value);
                case "string":
                    return Convert.ToString(value);
                case "datetime":
                    return Convert.ToDateTime(value);
                case "boolean":
                    return Convert.ToBoolean(value);
                case "char":
                    return Convert.ToChar(value);
                case "double":
                    return Convert.ToDouble(value);
                default:
                    return value;
            }
        }

        public async Task<decimal> GetPercentage(Guid id, int type)
        {
            var forminfo = _formInstructionRelationRepository.GetAll().Where(p => p.InstructionEntityId == id);
            var s = forminfo.Count();
            var percentage = 0m;
            if (forminfo.Count() > 0)
            {
                switch (type)
                {
                    case 1:
                        var a = forminfo.Where(p => p.IsImplement == 1).Count();
                        percentage = decimal.Parse(a.ToString()) / decimal.Parse(s.ToString()) * 100;
                        break;
                    case 2:
                        var b = forminfo.Where(p => p.IsFinsh == 1).Count();
                        percentage = decimal.Parse(b.ToString()) / decimal.Parse(s.ToString()) * 100;
                        break;
                    default:
                        break;
                }
            }
            return Math.Round(percentage, 2);
        }

        /// <summary>
        /// json回传
        /// </summary>
        /// <param name="id"></param>
        public void JsonExport(Guid id)
        {

            //  List<TableNameDictionary> tableNameDictionary = new List<TableNameDictionary>();
            //最终导出的工艺出参
            var datalist = new RJsonInput();

            var formInfos = _formInfoRepository.GetAll().Where(p => p.InstructionEntityId == id);
            //var forminfos = new RFormJsonInput();
            var forminfo = new RFormJsonInput();
            var ProductInfo = _instructionEntityRepository.FirstOrDefault(c => c.Id == id);


            var productInfoDto = ObjectMapper.Map<RProductInfoDto>(ProductInfo);
            productInfoDto.Id = new Guid(ProductInfo.OriginalId);
            datalist.ProductInfo = productInfoDto;

            foreach (var item in formInfos)
            {
                switch (item.FormName)
                {

                    case "实物配套表":
                        forminfo.MaterialObjectFormData = new RMaterialObjectFormEntityDataDto();
                        forminfo.MaterialObjectFormData.FormName = item.FormName;
                        var materialObjects = _materialObjectFormEntityRepository.GetAll().Where(c => c.FormInfoId == item.Id);
                        var materials = new List<RMaterialObjectFormEntityDto>();

                        foreach (var materialObject in materialObjects)
                        {
                            var materialObjectDto = ObjectMapper.Map<RMaterialObjectFormEntityDto>(materialObject);

                            var formInstructionRelation = _formInstructionRelationRepository.FirstOrDefault(c => c.FormId == materialObject.Id);

                            var formInstructionRelationDto = ObjectMapper.Map<GetFormInstructionRelationDto>(formInstructionRelation);


                            materials.Add(materialObjectDto);

                        }
                        forminfo.MaterialObjectFormData.MaterialObjectForm = materials;
                        break;
                    case "定量检查表":
                        forminfo.QuantitativeExaminationFormData = new RDingLiangExaminationFormEntityDataDto();
                        forminfo.QuantitativeExaminationFormData.FormName = item.FormName;
                        var dingLiangExaminations = _dingLiangExaminationFormEntityRepository.GetAll().Where(c => c.FormInfoId == item.Id);
                        var dingLiangs = new List<RDingLiangExaminationFormEntityDto>();

                        foreach (var dingLiangExamination in dingLiangExaminations)
                        {
                            var dingLiangsDto = ObjectMapper.Map<RDingLiangExaminationFormEntityDto>(dingLiangExamination);
                            dingLiangs.Add(dingLiangsDto);

                            //if (dingLiangsDto.Operator != "" || dingLiangsDto.MutualInspector !=  "" || dingLiangsDto.Inspector!= "")
                            //{
                            //    GetInspectionSignatureInfo(dingLiangsDto.Id, dingLiangsDto.Operator, dingLiangsDto.MutualInspector, dingLiangsDto.Inspector, productInfoDto.FileNum);
                            //}


                        }
                        forminfo.QuantitativeExaminationFormData.QuantitativeExaminationForm = dingLiangs;
                        break;
                    case "定性检查表":
                        forminfo.QualitativeExaminationFormData = new RDingXingExaminationFormEntityDataDto();
                        forminfo.QualitativeExaminationFormData.FormName = item.FormName;
                        var dingXingExaminations = _dingXingExaminationFormEntityRepository.GetAll().Where(c => c.FormInfoId == item.Id);
                        var dingXings = new List<RDingXingExaminationFormEntityDto>();
                        foreach (var dingXingExamination in dingXingExaminations)
                        {
                            var dingXingsDto = ObjectMapper.Map<RDingXingExaminationFormEntityDto>(dingXingExamination);

                            dingXings.Add(dingXingsDto);


                            //if (dingXingsDto.Operator !=  "" || dingXingsDto.MutualInspector !=  "" || dingXingsDto.Inspector !=  "")
                            //{
                            //    GetInspectionSignatureInfo(dingXingsDto.Id, dingXingsDto.Operator, dingXingsDto.MutualInspector, dingXingsDto.Inspector, productInfoDto.FileNum);
                            //}
                        }
                        forminfo.QualitativeExaminationFormData.QualitativeExaminationForm = dingXings;
                        break;
                    case "多媒体检查表":
                        forminfo.MultiMediaExaminationFormData = new RMultiMediaExaminationFormEntityDataDto();
                        forminfo.MultiMediaExaminationFormData.FormName = item.FormName;
                        var multiMediaDtos = new List<RMultiMediaExaminationFormEntityDto>();
                        var multiMediaExaminations = _multiMediaExaminationFormEntityRepository.GetAll().Where(c => c.FormInfoId == item.Id);
                        foreach (var multiMediaExamination in multiMediaExaminations)
                        {
                            var multiMediaDto = ObjectMapper.Map<RMultiMediaExaminationFormEntityDto>(multiMediaExamination);


                            multiMediaDtos.Add(multiMediaDto);

                            //if (multiMediaDto.MediaFileName !=  "") { GetMeasuredValue(multiMediaDto.Id, multiMediaDto.MediaFileName, productInfoDto.FileNum); }

                            //if (multiMediaDto.Operator !=  "" || multiMediaDto.MutualInspector !=  "" || multiMediaDto.Confirmer !=  "") { GetInspectionSignatureInfo(multiMediaDto.Id, multiMediaDto.Operator, multiMediaDto.Confirmer, multiMediaDto.MutualInspector, productInfoDto.FileNum); }

                        }
                        forminfo.MultiMediaExaminationFormData.MultiMediaExaminationForm = multiMediaDtos;
                        break;
                    case "定量&多媒体检查表":
                        forminfo.QuantitativeAndMultiMediaExaminationFormData = new RDingLiangAndMultiMediaDataDto();
                        forminfo.QuantitativeAndMultiMediaExaminationFormData.FormName = item.FormName;
                        var dingLiangAndMultiMedias = _dingLiangAndMultiMediaExaminationFormEntityRepository.GetAll().Where(c => c.FormInfoId == item.Id);

                        var dingLiangAndMultiMediaDtos = new List<RDingLiangAndMultiMediaDto>();
                        foreach (var dingLiangAndMultiMedia in dingLiangAndMultiMedias)
                        {
                            var dingLiangAndMultiMediaDto = ObjectMapper.Map<RDingLiangAndMultiMediaDto>(dingLiangAndMultiMedia);

                            //if (dingLiangAndMultiMediaDto.MediaFileName !=  "")
                            //{
                            //    GetMeasuredValue(dingLiangAndMultiMediaDto.Id, dingLiangAndMultiMediaDto.MediaFileName, productInfoDto.FileNum);
                            //}

                            //if (dingLiangAndMultiMediaDto.Operator !=  "" || dingLiangAndMultiMediaDto.MutualInspector !=  "" || dingLiangAndMultiMediaDto.Inspector !=  "")
                            //{
                            //    GetInspectionSignatureInfo(dingLiangAndMultiMediaDto.Id, dingLiangAndMultiMediaDto.Operator, dingLiangAndMultiMediaDto.Inspector, dingLiangAndMultiMediaDto.MutualInspector, productInfoDto.FileNum);
                            //}


                            dingLiangAndMultiMediaDtos.Add(dingLiangAndMultiMediaDto);
                        }
                        forminfo.QuantitativeAndMultiMediaExaminationFormData.QuantitativeAndMultiMediaExaminationForm = dingLiangAndMultiMediaDtos;
                        break;
                    case "力矩类定量检查表":
                        forminfo.MomentRationExaminationFormData = new RMomentRationExaminationFormEntityDataDto();
                        forminfo.MomentRationExaminationFormData.FormName = item.FormName;
                        var momentRationExaminationDtos = new List<RMomentRationExaminationFormEntityDto>();
                        var momentRationExaminations = _momentRationExaminationFormEntityRepository.GetAll().Where(c => c.FormInfoId == item.Id);
                        foreach (var momentRationExamination in momentRationExaminations)
                        {
                            {
                                var momentRationExaminationDto = ObjectMapper.Map<RMomentRationExaminationFormEntityDto>(momentRationExamination);
                                //if (momentRationExaminationDto.Operator !=  "" || momentRationExaminationDto.MutualInspector !=  "" || momentRationExaminationDto.Inspector !=  "")
                                //{
                                //    GetInspectionSignatureInfo(momentRationExaminationDto.Id, momentRationExaminationDto.Operator, momentRationExaminationDto.Inspector, momentRationExaminationDto.MutualInspector, productInfoDto.FileNum);
                                //}

                                momentRationExaminationDtos.Add(momentRationExaminationDto);
                            }
                        }
                        forminfo.MomentRationExaminationFormData.MomentRationExaminationForm = momentRationExaminationDtos;
                        break;
                    case "测试类定量检查表":  //_testRationExaminationFormEntityRepository
                        forminfo.TestRationExaminationFormData = new RTestRationExaminationFormEntityDataDto();
                        forminfo.TestRationExaminationFormData.FormName = item.FormName;
                        var testRationExaminationFormDtos = new List<RTestRationExaminationFormEntityDto>();
                        var testRationExaminations = _testRationExaminationFormEntityRepository.GetAll().Where(c => c.FormInfoId == item.Id);
                        foreach (var testRationExamination in testRationExaminations)
                        {
                            {
                                var testRationExaminationDto = ObjectMapper.Map<RTestRationExaminationFormEntityDto>(testRationExamination);
                                //if (testRationExaminationDto.Operator !=  "" || testRationExaminationDto.MutualInspector !=  "" || testRationExaminationDto.Inspector !=  "")
                                //{
                                //    GetInspectionSignatureInfo(testRationExaminationDto.Id, testRationExaminationDto.Operator, testRationExaminationDto.Inspector, testRationExaminationDto.MutualInspector, productInfoDto.FileNum);
                                //}

                                testRationExaminationFormDtos.Add(testRationExaminationDto);
                            }
                        }
                        forminfo.TestRationExaminationFormData.TestRationExaminationForm = testRationExaminationFormDtos;
                        break;
                    case "电连接器类多媒体":   //_electricalConnectorFormEntityRepository
                        var electricalConnectorDtos = new List<RElectricalConnectorFormEntityDto>();
                        forminfo.ElectricalConnectorFormData = new RElectricalConnectorFormEntityDataDto();
                        forminfo.ElectricalConnectorFormData.FormName = item.FormName;
                        var electricalConnectors = _electricalConnectorFormEntityRepository.GetAll().Where(c => c.FormInfoId == item.Id);
                        foreach (var electricalConnector in electricalConnectors)
                        {
                            {
                                var electricalConnectorDto = ObjectMapper.Map<RElectricalConnectorFormEntityDto>(electricalConnector);

                                //if (electricalConnectorDto.Notekeeper !=  "" || electricalConnectorDto.Confirmer !=  "" || electricalConnectorDto.Inspector !=  "")
                                //{

                                //    GetInspectionSignatureInfo(electricalConnectorDto.Id, electricalConnectorDto.Notekeeper, electricalConnectorDto.Inspector, electricalConnectorDto.Confirmer, productInfoDto.FileNum);
                                //}

                                electricalConnectorDtos.Add(electricalConnectorDto);
                            }
                        }
                        forminfo.ElectricalConnectorFormData.ElectricalConnectorForm = electricalConnectorDtos;
                        break;
                    default: break;
                }


                //forminfos.Add(forminfo);

            }
            datalist.FormInfo = forminfo;

            var listjson = Newtonsoft.Json.JsonConvert.SerializeObject(datalist);
            string filesavePath = Environment.CurrentDirectory + "/wwwroot/outJson" + "/" + productInfoDto.FileNum + "/" + productInfoDto.FileNum + ".json";
            string fp = filesavePath;
            if (File.Exists(fp))  // 判断是否已有相同文件 
            {
                File.Delete(fp);
            }
            File.WriteAllText(filesavePath, listjson);

        }
        /// <summary>
        /// 获取多媒体信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="measuredValues"></param>
        /// <param name="fileNum"></param>
        private void GetMeasuredValue(Guid id, string measuredValues, string fileNum)
        {
            var formInstructionRelation = _formInstructionRelationRepository.FirstOrDefault(c => c.FormId == id);
            WebClient wc = new WebClient();
            var formInstructionRelationDto = ObjectMapper.Map<GetFormInstructionRelationDto>(formInstructionRelation);
            string filesavePath = Environment.CurrentDirectory + "/wwwroot/outJson" + "/" + fileNum;

            if (!Directory.Exists(filesavePath))
            {
                Directory.CreateDirectory(filesavePath);

            }


            var techniqueEntity = _techniqueEntityRepository.FirstOrDefault(c => c.Id == formInstructionRelation.TechniqueEntityId);

            var Values = measuredValues.Split(',');
            var srcPatch = "";
            foreach (var item in Values)
            {

                var format = item.Split('.')[1].Trim();
                var name = item.Split('.')[0].Trim();
                if (format == "jpeg")
                {
                    if (!Directory.Exists(filesavePath + "/picture"))
                    {
                        Directory.CreateDirectory(filesavePath + "/picture");

                    }
                    //   srcPatch = Environment.CurrentDirectory + "\\wwwroot\\DownLoadFileRootPath/" + techniqueEntity.TechniqueNumber + "/picture/"+item;
                    srcPatch = Environment.CurrentDirectory + "\\wwwroot\\UploadFile/" + techniqueEntity.TechniqueNumber + "/picture/" + item;
                    string fp = filesavePath + "/picture/" + name + "." + format + ".jpeg";
                    if (File.Exists(fp))  // 判断是否已有相同文件 
                    {
                        File.Delete(fp);
                    }
                    wc.DownloadFile(srcPatch, filesavePath + "/picture/" + name + "." + format);
                }
                if (format == "mp4")
                {

                    if (!Directory.Exists(filesavePath + "/video"))
                    {
                        Directory.CreateDirectory(filesavePath + "/video");

                    }
                    srcPatch = Environment.CurrentDirectory + "\\wwwroot\\UploadFile/" + techniqueEntity.TechniqueNumber + "/video/" + item;

                    string fp = filesavePath + "/video/" + name + "." + format + ".mp4";
                    if (File.Exists(fp))  // 判断是否已有相同文件 
                    {
                        File.Delete(fp);
                    }
                    wc.DownloadFile(srcPatch, filesavePath + "/video/" + name + "." + format);
                }

            }



        }


        private void GetInspectionSignatureInfo(Guid id, string operators, string confirmer, string inspector, string fileNum)
        {
            var formInstructionRelation = _formInstructionRelationRepository.FirstOrDefault(c => c.FormId == id);
            WebClient wc = new WebClient();
            var formInstructionRelationDto = ObjectMapper.Map<GetFormInstructionRelationDto>(formInstructionRelation);
            string filesavePath = Environment.CurrentDirectory + "/wwwroot/outJson" + "/" + fileNum;

            if (!Directory.Exists(filesavePath))
            {
                Directory.CreateDirectory(filesavePath);
            }
            var techniqueEntity = _techniqueEntityRepository.FirstOrDefault(c => c.Id == formInstructionRelation.TechniqueEntityId);
            var operatorsUserDto = new List<UserJsonDto>();
            var confirmerUserDto = new List<UserJsonDto>();
            var inspectorUserDto = new List<UserJsonDto>();
            if (operators != "" && operators != null)
            {
                operatorsUserDto = JsonConvert.DeserializeObject<List<UserJsonDto>>(operators.Replace(" ", ""));
            }
            if (confirmer != "" && confirmer != null)
            {
                confirmerUserDto = JsonConvert.DeserializeObject<List<UserJsonDto>>(confirmer.Replace(" ", ""));
            }
            if (inspector != "" && inspector != null)
            {
                inspectorUserDto = JsonConvert.DeserializeObject<List<UserJsonDto>>(inspector.Replace(" ", ""));
            }
            var names = new List<string>();
            foreach (var item in operatorsUserDto)
            {
                var name = "";
                name = item.Name;
                names.Add(name);
            }
            foreach (var item in confirmerUserDto)
            {
                var name = "";
                name = item.Name;
                names.Add(name);
            }
            foreach (var item in inspectorUserDto)
            {
                var name = "";
                name = item.Name;
                names.Add(name);
            }


            //names.Add(operators);
            //names.Add(inspector);
            //names.Add(confirmer);
            if (!Directory.Exists(filesavePath + "/signature"))
            {
                Directory.CreateDirectory(filesavePath + "/signature");
            }
            foreach (var item in names)
            {
                var format = item.Split(".")[1].Trim();
                var name = item.Split('.')[0].Trim();
                var srcPatch = Environment.CurrentDirectory + "\\wwwroot\\UploadFile/" + techniqueEntity.TechniqueNumber + "/signature/" + item;
                string fp = filesavePath + "/signature/" + name + "." + format; //+ ".png";
                if (File.Exists(fp))  // 判断是否已有相同文件 
                {
                    File.Delete(fp);
                }

                wc.DownloadFile(srcPatch, filesavePath + "/signature/" + name + "." + format);
            }




        }

        public async Task<PagedResultDto<GetZuoYeDetailDataOutput>> GetTableFormDetail(GetZuoYeDetailInput input)
        {
            var table = new List<GetZuoYeDetailDataOutput>();
            var count = 0;
            var tcol = _formRelationEntityRepository.FirstOrDefault(p => p.TestProjectNum == input.TestProjectNum);
            if (tcol != null)
            {
                var query = _tableBaseRepository.GetAllIncluding(a => a.InspectionSignature, b => b.MeasuredResult, i => i.MultiMedia, e => e.RecordRequirements).Where(c => c.ProcessesEntityId == input.Id && c.FormName == tcol.FormName)
               .WhereIf(!input.Filter.IsNullOrWhiteSpace(),
               x => x.ParamCode.Contains(input.Filter)
               );
                count = await query.CountAsync();
                var instructionlist = await query.OrderBy(p => p.SerialNum).PageBy(input).ToListAsync();
                table = ObjectMapper.Map<List<GetZuoYeDetailDataOutput>>(instructionlist);

            }
            return new PagedResultDto<GetZuoYeDetailDataOutput>(count, table);
        }


        public async Task<PagedResultDto<GetZuoYeDetailDataOutput>> GetRTableFormDetail(GetZuoYeDetailInput input)
        {
            var table = new List<GetZuoYeDetailDataOutput>();
            var count = 0;
            var tcol = _formRelationEntityRepository.FirstOrDefault(p => p.TestProjectNum == input.TestProjectNum);
            if (tcol != null)
            {
                var query = _rtableBaseRepository.GetAllIncluding(a => a.InspectionSignature, b => b.MeasuredResult, i => i.MultiMedia, e => e.RecordRequirements).Where(c => c.RProcessesEntityId == input.Id && c.FormName == tcol.FormName)
                .WhereIf(!input.Filter.IsNullOrWhiteSpace(),
                x => x.ParamCode.Contains(input.Filter)
                );
                count = await query.CountAsync();
                var instructionlist = await query.OrderBy(p => p.SerialNum).PageBy(input).ToListAsync();
                var tables = ObjectMapper.Map<List<GetZuoYeDetailDataOutput>>(instructionlist);
                table = GetPhoto(tables);
            }
            foreach (var item in table)
            {
                item.Operators = new List<UserSignatureInfoDto>();
                item.MutualInspectors = new List<UserSignatureInfoDto>();
                item.Inspectors = new List<UserSignatureInfoDto>();
                var data = GetUserSignatureInfo(item.FirstOperator, item.SecondOperator, item.ThirdOperator);
                if (data.First != null)
                {
                    item.Operators = data.First;
                }
                if (data.Second != null)
                {
                    item.MutualInspectors = data.Second;
                }
                if (data.Third != null)
                {
                    item.Inspectors = data.Third;
                }
                //if (data != null)
                //{
                //    item.Operators = data.First;
                //    item.MutualInspectors = data.Second;
                //    item.Inspectors = data.Third;
                //}
                //item.FirstOperator = JsonConvert.DeserializeObject<List<UserJsonDto>>(item.FirstOperator.Replace(" ", ""))[0].Name;
            }
            return new PagedResultDto<GetZuoYeDetailDataOutput>(count, table);
        }

        private static List<GetZuoYeDetailDataOutput> GetPhoto(List<GetZuoYeDetailDataOutput> table)
        {
            foreach (var item in table)
            {
                List<MediaFileList> PhotoNo = new List<MediaFileList>();
                if (item.PhotoNo != null && item.PhotoNo != "")
                {
                    var fileNames = item.PhotoNo.Split(',');
                    foreach (var fileName in fileNames)
                    {
                        var types = fileName.Split('.');
                        MediaFileList mediaFileList = new MediaFileList();
                        mediaFileList.MediaFileName = fileName;
                        mediaFileList.Type = types[1];
                        PhotoNo.Add(mediaFileList);

                    }
                }
                item.PhotoNoList = PhotoNo;
                List<MediaFileList> SecondPhotoNo = new List<MediaFileList>();
                if (item.SecondPhotoNo != null && item.SecondPhotoNo != "")
                {
                    var fileNames = item.SecondPhotoNo.Split(',');
                    foreach (var fileName in fileNames)
                    {
                        var types = fileName.Split('.');
                        MediaFileList mediaFileList = new MediaFileList();
                        mediaFileList.MediaFileName = fileName;
                        mediaFileList.Type = types[1];
                        SecondPhotoNo.Add(mediaFileList);

                    }
                }
                item.SecondPhotoNoList = SecondPhotoNo;
                List<MediaFileList> ThirdPhotoNo = new List<MediaFileList>();
                if (item.ThirdPhotoNo != null && item.ThirdPhotoNo != "")
                {
                    var fileNames = item.ThirdPhotoNo.Split(',');
                    foreach (var fileName in fileNames)
                    {
                        var types = fileName.Split('.');
                        MediaFileList mediaFileList = new MediaFileList();
                        mediaFileList.MediaFileName = fileName;
                        mediaFileList.Type = types[1];
                        ThirdPhotoNo.Add(mediaFileList);

                    }

                }
                item.ThirdPhotoNoList = ThirdPhotoNo;
                List<MediaFileList> FourthPhotoNo = new List<MediaFileList>();
                if (item.FourthPhotoNo != null && item.FourthPhotoNo != "")
                {

                    var fileNames = item.FourthPhotoNo.Split(',');
                    foreach (var fileName in fileNames)
                    {
                        var types = fileName.Split('.');
                        MediaFileList mediaFileList = new MediaFileList();
                        mediaFileList.MediaFileName = fileName;
                        mediaFileList.Type = types[1];
                        FourthPhotoNo.Add(mediaFileList);

                    }

                }
                item.FourthPhotoNoList = FourthPhotoNo;
            }
            return table;
        }


        public async Task<List<GetFromRelationOutPut>> GetFromRelationOutPut()
        {
            var formdatas = await _formRelationEntityRepository.GetAllListAsync();
            var i = 0;
            List<GetFromRelationOutPut> getFromRelationOutPuts = new List<GetFromRelationOutPut>();
            foreach (var formdata in formdatas)
            {
                var color1 = GetColor();
                GetFromRelationOutPut formdata1 = new GetFromRelationOutPut();
                formdata1.Name = formdata.TestProject;
                formdata1.Value = 240;
                formdata1.HiddenInLegend = false;
                if (i != 0)
                {
                    formdata1.Collapsed = true;
                }


                formdata1.Color = color1;

                var color = GetColor();
                List<GetFromRelationOutPut> getFromRelationOutPuts0 = new List<GetFromRelationOutPut>();
                List<GetFromRelationOutPut> getFromRelationOutPuts1 = new List<GetFromRelationOutPut>();
                GetFromRelationOutPut children1 = new GetFromRelationOutPut();
                children1.Name = formdata.FormName;
                children1.Value = 240;
                if (i != 0)
                {
                    children1.Collapsed = true;
                }
                children1.Color = color;
                children1.HiddenInLegend = true;

                var coldatas = await _formColRelationEntityRepository.GetAllListAsync(p => p.FormRelationEntityId == formdata.Id);
                foreach (var coldata in coldatas)
                {

                    List<GetFromRelationOutPut> getFromRelationOutPuts4 = new List<GetFromRelationOutPut>();
                    GetFromRelationOutPut children4 = new GetFromRelationOutPut();
                    children4.Name = coldata.ColTitle;
                    children4.Value = 100;
                    if (i != 0)
                    {
                        children4.Collapsed = true;
                    }
                    children4.Color = color1;
                    children4.HiddenInLegend = true;
                    getFromRelationOutPuts4.Add(children4);


                    GetFromRelationOutPut children2 = new GetFromRelationOutPut();
                    children2.Name = coldata.TechniqueFormColTitle;
                    children2.Value = 100;
                    if (i != 0)
                    {
                        children2.Collapsed = true;
                    }
                    children2.Color = color;
                    children2.HiddenInLegend = true;
                    children2.Children = getFromRelationOutPuts4;
                    getFromRelationOutPuts1.Add(children2);

                    //
                }
                children1.Children = getFromRelationOutPuts1;
                getFromRelationOutPuts0.Add(children1);
                formdata1.Children = getFromRelationOutPuts0;
                getFromRelationOutPuts.Add(formdata1);
                i = i + 1;
            }
            return getFromRelationOutPuts;
        }
        public string GetColor()
        {
            //Random RandomNum_First = new Random((int)DateTime.Now.Ticks);

            // //  对于C#的随机数，没什么好说的

            // System.Threading.Thread.Sleep(RandomNum_First.Next(50));

            //Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks);



            // //  为了在白色背景上显示，尽量生成深色

            // int int_Red = RandomNum_First.Next(256);

            // int int_Green = RandomNum_Sencond.Next(256);

            // int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;

            // int_Blue = (int_Blue > 255) ? 255 : int_Blue;
            //             Color color = Color.FromArgb(int_Red, int_Green, int_Blue);
            //             string strColor = "#" + Convert.ToString(color.ToArgb(), 16).PadLeft(8, '0').Substring(2, 6);
            //             return strColor;
            Random randomNum_1 = new Random(Guid.NewGuid().GetHashCode());
            System.Threading.Thread.Sleep(randomNum_1.Next(1));
            int int_Red = randomNum_1.Next(255);

            Random randomNum_2 = new Random((int)DateTime.Now.Ticks);
            int int_Green = randomNum_2.Next(255);

            Random randomNum_3 = new Random(Guid.NewGuid().GetHashCode());

            int int_Blue = randomNum_3.Next(255);
            int_Blue = (int_Red + int_Green > 380) ? int_Red + int_Green - 380 : int_Blue;
            int_Blue = (int_Blue > 255) ? 255 : int_Blue;
            Color color = Color.FromArgb(int_Red, int_Green, int_Blue);
            return "#" + Convert.ToString(color.ToArgb(), 16).PadLeft(8, '0').Substring(2, 6);
        }


        public async Task<InstructionInfoDto> GetAllProductInfoToTable(GetAllProductInfosInput input)
        {
            var productInfos = new InstructionInfoDto();

            var formInfos = _formInfoRepository.GetAll().Where(p => p.InstructionEntityId == input.Id);

            var instructionEntity = await _instructionEntityRepository.FirstOrDefaultAsync(c => c.Id == input.Id);

            var InstructionInfo = ObjectMapper.Map<GetProductInfoOutputDto>(instructionEntity);

            productInfos.ProductInfo = InstructionInfo;

            //配套
            var materialObjectForm = await _formInfoRepository.FirstOrDefaultAsync(p => p.InstructionEntityId == input.Id && p.FormName == "实物配套表");
            var materialObjectForms = new List<GetMaterialObjectFormEntityDto>();
            if (materialObjectForm != null)
            {
                var query = _materialObjectFormEntityRepository.GetAll().Where(p => p.FormInfoId == materialObjectForm.Id);
                materialObjectForms = ObjectMapper.Map<List<GetMaterialObjectFormEntityDto>>(query);
                foreach (var item in materialObjectForms)
                {
                    var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == item.Id);
                    item.FormInstructionRelation = ObjectMapper.Map<GetFormInstructionRelationDto>(formInstruction);
                }
            }
            productInfos.MaterialObjectForms = materialObjectForms;
            //定量检查表
            var dingLiangExaminationForm = await _formInfoRepository.FirstOrDefaultAsync(p => p.InstructionEntityId == input.Id && p.FormName == "定量检查表");
            var dingLiangExaminationForms = new List<GetDingLiangExaminationFormEntityDto>();
            if (dingLiangExaminationForm != null)
            {
                var query = _dingLiangExaminationFormEntityRepository.GetAll().Where(p => p.FormInfoId == dingLiangExaminationForm.Id);
                dingLiangExaminationForms = ObjectMapper.Map<List<GetDingLiangExaminationFormEntityDto>>(query);
                foreach (var item in dingLiangExaminationForms)
                {
                    var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == item.Id);
                    item.FormInstructionRelation = ObjectMapper.Map<GetFormInstructionRelationDto>(formInstruction);
                }
            }
            productInfos.DingLiangExaminationForms = dingLiangExaminationForms;
            //  定性检查表
            var dingXingExaminationForm = await _formInfoRepository.FirstOrDefaultAsync(p => p.InstructionEntityId == input.Id && p.FormName == "定性检查表");

            var dingXingExaminationForms = new List<GetDingXingExaminationFormEntityDto>();
            if (dingXingExaminationForm != null)
            {
                var query = _dingXingExaminationFormEntityRepository.GetAll().Where(p => p.FormInfoId == dingXingExaminationForm.Id);

                dingXingExaminationForms = ObjectMapper.Map<List<GetDingXingExaminationFormEntityDto>>(query);
                foreach (var item in dingXingExaminationForms)
                {
                    var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == item.Id);
                    item.FormInstructionRelation = ObjectMapper.Map<GetFormInstructionRelationDto>(formInstruction);
                }
            }
            productInfos.DingXingExaminationForms = dingXingExaminationForms;
            //多媒体检查表
            var multiMediaExaminationForm = await _formInfoRepository.FirstOrDefaultAsync(p => p.InstructionEntityId == input.Id && p.FormName == "多媒体检查表");
            var multiMediaExaminationForms = new List<GetJSMultiMediaExaminationFormEntityDto>();
            if (multiMediaExaminationForm != null)
            {
                var query = _multiMediaExaminationFormEntityRepository.GetAll().Where(p => p.FormInfoId == multiMediaExaminationForm.Id);
                multiMediaExaminationForms = ObjectMapper.Map<List<GetJSMultiMediaExaminationFormEntityDto>>(query);
                foreach (var item in multiMediaExaminationForms)
                {
                    var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == item.Id);
                    item.FormInstructionRelation = ObjectMapper.Map<GetFormInstructionRelationDto>(formInstruction);
                    List<MediaFileList> mediaFileLists = new List<MediaFileList>();
                    if (item.MediaFileName != null && item.MediaFileName != "")
                    {
                        var fileNames = item.MediaFileName.Split(',');
                        foreach (var fileName in fileNames)
                        {
                            var types = fileName.Split('.');
                            MediaFileList mediaFileList = new MediaFileList();
                            mediaFileList.MediaFileName = fileName;
                            mediaFileList.Type = types[1];
                            mediaFileLists.Add(mediaFileList);

                        }
                    }
                    item.MediaFileLists = mediaFileLists;
                }
            }
            productInfos.MultiMediaExaminationForms = multiMediaExaminationForms;
            //定量&多媒体检查表

            var dingLiangAndMultiMedia = await _formInfoRepository.FirstOrDefaultAsync(p => p.InstructionEntityId == input.Id && p.FormName == "定量&多媒体检查表");
            var dingLiangAndMultiMedias = new List<GetJSDingLiangAndMultiMediaDto>();
            if (dingLiangAndMultiMedia != null)
            {
                var query = _dingLiangAndMultiMediaExaminationFormEntityRepository.GetAll().Where(p => p.FormInfoId == dingLiangAndMultiMedia.Id);

                dingLiangAndMultiMedias = ObjectMapper.Map<List<GetJSDingLiangAndMultiMediaDto>>(query);
                foreach (var item in dingLiangAndMultiMedias)
                {
                    var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == item.Id);
                    item.FormInstructionRelation = ObjectMapper.Map<GetFormInstructionRelationDto>(formInstruction);
                    List<MediaFileList> mediaFileLists = new List<MediaFileList>();
                    if (item.MediaFileName != null && item.MediaFileName != "")
                    {
                        var fileNames = item.MediaFileName.Split(',');
                        foreach (var fileName in fileNames)
                        {
                            var types = fileName.Split('.');
                            MediaFileList mediaFileList = new MediaFileList();
                            mediaFileList.MediaFileName = fileName;
                            mediaFileList.Type = types[1];
                            mediaFileLists.Add(mediaFileList);

                        }
                    }
                    item.MediaFileLists = mediaFileLists;
                }
            }
            productInfos.DingLiangAndMultiMedias = dingLiangAndMultiMedias;
            //力矩类定量检查表
            var momentRationExaminationForm = await _formInfoRepository.FirstOrDefaultAsync(p => p.InstructionEntityId == input.Id && p.FormName == "力矩类定量检查表");
            var momentRationExaminationForms = new List<GetMomentRationExaminationFormEntityDto>();
            if (momentRationExaminationForm != null)
            {
                var query = _momentRationExaminationFormEntityRepository.GetAll().Where(p => p.FormInfoId == momentRationExaminationForm.Id);

                momentRationExaminationForms = ObjectMapper.Map<List<GetMomentRationExaminationFormEntityDto>>(query);
                foreach (var item in momentRationExaminationForms)
                {
                    var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == item.Id);
                    item.FormInstructionRelation = ObjectMapper.Map<GetFormInstructionRelationDto>(formInstruction);
                }
            }
            productInfos.MomentRationExaminationForms = momentRationExaminationForms;
            //测试类定量检查表
            var testRationExaminationForm = await _formInfoRepository.FirstOrDefaultAsync(p => p.InstructionEntityId == input.Id && p.FormName == "测试类定量检查表");
            var testRationExaminationForms = new List<GetTestRationExaminationFormEntityDto>();
            if (testRationExaminationForm != null)
            {
                var query = _testRationExaminationFormEntityRepository.GetAll().Where(p => p.FormInfoId == testRationExaminationForm.Id);

                testRationExaminationForms = ObjectMapper.Map<List<GetTestRationExaminationFormEntityDto>>(query);
                foreach (var item in testRationExaminationForms)
                {
                    var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == item.Id);
                    item.FormInstructionRelation = ObjectMapper.Map<GetFormInstructionRelationDto>(formInstruction);
                }
            }
            productInfos.TestRationExaminationForms = testRationExaminationForms;
            //电连接器类多媒体
            var electricalConnectorForm = await _formInfoRepository.FirstOrDefaultAsync(p => p.InstructionEntityId == input.Id && p.FormName == "电连接器类多媒体");
            var electricalConnectorForms = new List<GetJSElectricalConnectorFormEntityDto>();
            if (electricalConnectorForm != null)
            {
                var query = _electricalConnectorFormEntityRepository.GetAll().Where(p => p.FormInfoId == electricalConnectorForm.Id);
                electricalConnectorForms = ObjectMapper.Map<List<GetJSElectricalConnectorFormEntityDto>>(query);
                foreach (var item in electricalConnectorForms)
                {
                    var formInstruction = await _formInstructionRelationRepository.FirstOrDefaultAsync(p => p.FormId == item.Id);
                    item.FormInstructionRelation = ObjectMapper.Map<GetFormInstructionRelationDto>(formInstruction);
                    List<MediaFileList> mediaFileLists = new List<MediaFileList>();
                    if (item.MediaFileName != null && item.MediaFileName != "")
                    {
                        var fileNames = item.MediaFileName.Split(',');
                        foreach (var fileName in fileNames)
                        {
                            var types = fileName.Split('.');
                            MediaFileList mediaFileList = new MediaFileList();
                            mediaFileList.MediaFileName = fileName;
                            mediaFileList.Type = types[1];
                            mediaFileLists.Add(mediaFileList);

                        }
                    }
                    item.MediaFileLists = mediaFileLists;
                }
            }
            productInfos.ElectricalConnectorForms = electricalConnectorForms;
            return productInfos;
        }

        public async Task<GetProductInfoOutputDto> GetProductInfo(Guid id)
        {
            var query = await _instructionEntityRepository.FirstOrDefaultAsync(c => c.Id == id);
            //var count = await query.CountAsync();
            //var instructionlist = await query.OrderBy(input.Sorting).PageBy(input).ToListAsync();
            var productInfo = ObjectMapper.Map<GetProductInfoOutputDto>(query);
            var user = await UserManager.GetUserByIdAsync(productInfo.CreatorUserId);
            productInfo.CreationUserName = user.Surname + user.Name;
            return productInfo;
        }

    }
}